Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989. It has been used as the default login shell for most Linux distributions. Bash was one of the first programs Linus Torvalds ported to Linux, alongside GCC.
Bash shell is the default shell in Linux in redhat based Linux distributions and majority of the Linux distributions where users not need to install it separately.
Bash is a shell compatible command line interpreter that executes commands from standard input or from a file. Bash has become by far the most popular shell among users of Linux, becoming the default interactive shell on that operating system’s various distributions and on Apple’s macOS releases before Catalina in October 2019. Bash has also been ported to Microsoft Windows and distributed with Cygwin and MinGW, to DOS by the DJGPP project, to Novell NetWare, to OpenVMS by the GNV project to ArcaOS, and to Android via various terminal emulation applications.
When a user presses the tab key within an interactive command-shell, Bash automatically uses command line completion to match partly typed program names, filenames and variable names. The Bash command-line completion system is very flexible and customizable, and is often packaged with functions that complete arguments and filenames for specific programs and tasks.
When using the ‘function’ keyword, Bash function declarations are not compatible with Bourne/Korn/POSIX scripts but Bash accepts the same function declaration syntax as the Bourne and Korn shells, and is POSIX-conformant. Because of these and other differences, Bash shell scripts are rarely runnable under the Bourne or Korn shell interpreters unless deliberately written with that compatibility in mind, which is becoming less common as Linux becomes more widespread. But in POSIX mode, Bash conforms with POSIX more closely.
What is the difference between sh (shell) and bash (Bourne again shell):
Both sh and bash commands are same. sh is the executable file for bash command which is located in /usr/bin/sh in redhat based Linux distributions. While using sh to execute commands and scripts, it invokes the default shell available in the operating system which is bash in majority of the Linux distributions. Apart from this all the options are same for sh and bash. If you check the manual of sh and bash you will find both are having same options. In fact you will find name of the manual for sh is bash.
Features of Bash:
- Create bash scripts and invoke those scripts as commands by using their file names
- Command line arguments to the shell script
- Using of flow control statements.
- Creating local and global variables.
- Redirection of input and output values, using of pipelines.
- Commands can be executed using back ticks (`) or $ symbol.
- Use of HERE documents to embed a block of text in a script
Bash command options with examples:
How to read input from standard input:
To execute any command using bash use -c option to pass the command of command arguments. If there are arguments after the command string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters. The assignment to $0 sets the name of the shell, which is used in warning and error messages.
$ bash -c date
Wed Oct 26 01:16:54 EDT 2022
$ bash -c ls
anaconda-ks.cfg Documents du_command_examples.sh initial-setup-ks.cfg Pictures reports.txt Student_data1 test_1.txt test_data test.py Videos
Desktop Downloads file.txt Music Public scripts Templates test_2 test_file.txt test.txt youu
If you want to execute the command with arguments put the command and arguments in single quotes, then bash command will treat it as a single command and execute it and return the output. If single quotes are not used then bash treats it as a multiple commands and it will try to execute those commands which will fail if the passed arguments are not commands.
$ bash -c ‘cat /var/tmp/reports.txt’
New data
Test line 1
Test line 2
Test line 3
Test line 4
Test line 5
Test line 9
Interactive and non-interactive shells:
Interactive shell:
An interactive shell is used to execute the commands manually from standard input by the user. With interactive shell a user can run the script and pass the input to the script if required from standard input. We can’t use interactive shells for executing shell script in background by scheduling.
The default in all Linux distributions is always in interactive mode because to execute the commands and get the output. If you want explicitly try using the shell in interactive mode use -i option. As you are already in interactive mode by default and by using -i option you wont get that much difference while executing commands.
$ bash -i
$ ls
$ date
$ free
$ exit
Non-Interactive shell:
An non interactive shell is used to execute the commands in background. Interactive shells doesn’t interact with the user. It is useful to run the scripts in background and for some of the automated processes.
While working with scripts in interactive shells use the absolute paths in the shell scripts instead of relative paths. Please find the more details about absolute path and relative paths in this article about mkdir command options with examples. If you are using relative path, then shell script will fail because of the file path is not sufficient to check the file.
See also:
How to login to new shell in bash:
While working in bash shell you can login to new shell and execute the command in that shell. All the commands we are executing will run inside that new login shell. To come out of the new shell or to logout from the new shell use exit command. Now user is in default shell of the user.
When bash is invoked as an interactive login shell, or as a non-interactive shell with the –login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The –noprofile option may be used when the shell is started to inhibit this behavior.
$ bash -
$ bash --login
$ bash --login
$ ls
$ date
$ cat /var/tmp/reports.txt
$ exit
How to start bash in restricted mode:
If bash command is executed with -r option, the shell is in restricted mode. A restricted shell is used to set up an environment more controlled than the standard shell. It behaves identically to bash with the exception that the following are disallowed or not performed in restricted mode.
- Changing of directories with cd
- setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV
- specifying command names containing /
- specifying a filename containing a / as an argument to the . builtin command
- specifying a filename containing a slash as an argument to the -p option to the hash builtin command
- importing function definitions from the shell environment at startup
- parsing the value of SHELLOPTS from the shell environment at startup
- redirecting output using the >, >|, <>, >&, &>, and >> redirection operators
- using the exec builtin command to replace the shell with another command
- adding or deleting builtin commands with the -f and -d options to the enable builtin command
- using the enable builtin command to enable disabled shell builtins
- specifying the -p option to the command builtin command
- turning off restricted mode with set +r or set +o restricted.
To come out of the restricted mode in bash use exit command.
$ bash -r
$ bash --restricted
[test_user@localhost ~]$ bash -r
[test_user@localhost ~]$
[test_user@localhost ~]$ cd /tmp
bash: cd: restricted
[test_user@localhost ~]$
[test_user@localhost ~]$
[test_user@localhost ~]$ export PATH='/opt/test/'
bash: PATH: readonly variable
[test_user@localhost ~]$ exit
Quoting in Bash:
Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.
Each of the metacharacters listed above under DEFINITIONS has special meaning to the shell and must be quoted if it is to represent itself. When the command history expansion facilities are being used (see HISTORY EXPANSION below), the history expansion character, usually !, must be quoted to prevent history expansion.
There are three quoting mechanisms: the escape character, single quotes, and double quotes. A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception of <newline>. If a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).
Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
\a alert (bell)
\b backspace
\e
\E an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\" double quote
\? question mark
\nnn the eight-bit character whose value is the octal value nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
\uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)
\UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)
\cx a control-x character
Print verbose messages in bash command:
To check what is happening in background in bash command use -v or –verbose option. It will list all the backround tasks that are executing as part of the bash command execution. It is useful to troubleshoot if facing any issue while executing the bash command and to check what are the environment variables are being set for bash and what are the startup scripts are executing in bash command.
$ bash -v
$ bash --verbose
There are lots of other options available in bash like pipelines, Lists, compound commands, coprocesses, shell functions which are called as shell grammar. For more information about these options please check the details in this bash command manual page.