4. Working with Bash Shell

How does a shell fit into the overall picture:

Understanding Redirection

  • Redirection is used to manipulate input and output of commands

  • Standard input (0): <

    • sort < /etc/services

  • Standard output (1): >

    • ls > ~/myfile=> deletes the old one and creates a new one

    • who >> ~/myfile => appends to the old one

  • Standard error (2): 2>

    • grep -R root /proc 2>/dev/null: if you don't want to see the error

    • grep -R root /etc &> ~/myfile: it will dump output+errors to the file

Understanding Variables

  • A variable is a label to which a dynamic value can be assigned.

  • System variables contain default settings used by Linux

  • Environment variables can be set for application use:

    • Use varname=value to define

    • Use echo $varname to read

  • By default, variables are only known to the current shell.

    • Use export to export it to all subshells.

The env variable contains a lot of information:

The environment variable - env
You can refer to the env variables

Using export to make variable available in subshells:

Use export to make variable available in subshells

Alias in Linux

  • alias allows you to define your own commands. It is handy when you type in some long commands very frequently.

  • By default set through /etc/profile

Bash also includes convenient keyboard shortcuts:

  • Ctrl+l clear screen

  • Ctrl+u wipe current command line

  • Ctrl+a move to the beginning of a line

  • Ctrl+e move to the end of a line

  • Ctrl+c interrupt the current process

  • Ctrl+d Exit

Aliases set in the current environment - alias:

Making your own aliases but non-persistent because it's local to the current shell:

Setting up your own alias, the non-persistent way.

Remember to use double quotes when your alias contain a space. for example $ alias s="ssh user@192.168.4.80".

To make it persistent, you can edit the ~/.bashrc file and put the aliases there.

Capture from tecmint.com

Bash Startup Files

Bash startup files are the configuration files that you can use to store your configurations like aliases, variables, etc. persistent.

  • /etc/environment contains a list of variables and is the first file that is processed while starting bash (empty by default on Red Hat). It is a system-wide configuration file, which means it is used by all users but owned by root.

  • /etc/profile is executed while users login - for all users. But to make changes you need to add/modify in /etc/profile.d/ directory for all users.

    • /etc/profile.d is used as a snap-in directory that contains additional configuration

    • ~/.bash_profile can be used as a user-specific version

    • ~/.bash_logout is processed when a user logs out

  • /etc/bashrc is processed every time a subshell is started

    • A user-specific ~/.bashrc file may be used.

Lab

  • Modify the environment so that after login, all users have access to the following:

    • An alias with the name ipconfig that runs the ip addr show command.

    • A variable with the name COLOR that is set to value red

    • Ensure the alias is available in subshells as well

Lab Solutions

1. Modify the environment so that after login, all users have access to the following:

  • An alias with the name ipconfig that runs the ip addr show command.

  • A variable with the name COLOR that is set to value red

  • Ensure the alias is available in subshells as well

Notice the variable and alias are available in the subshells well:

‚Äč