The bash (Bourne-Again SHell) is a sh-compatible command-line interpreter that reads from standard input or from a file and gives you the resulting output.
We have already written an article about bash and its differences from the standard POSIX shell.
Today, you will learn how to use the bash command in Linux with different options to run commands or scripts.
|Description||Bash (Bourne Again Shell)|
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
Syntax of the Bash Command
The bash command takes two arguments: one is the option, and the other is the command string or file path.
$ bash [OPTION] [COMMAND STRING/FILE PATH]
Starting an Interactive Shell Session
When you execute the “
bash” command with “
-i” or without any flag in your terminal, it will start a new bash process.
If your default shell is bash, then you will find multiple bash processes running on your system.
$ ps PID TTY TIME CMD 42753 pts/1 00:00:00 bash 87029 pts/1 00:00:00 bash 87035 pts/1 00:00:00 ps
Note that when you start the bash interactive shell session, it will load the “
~/.bashrc” configuration file.
Starting an Interactive Shell Session without User Configuration
Use the “
--norc” flag to run the interactive shell session without reading or executing the startup configuration file (“
$ bash --norc
Invoked Bash as the Login Shell
-l” or “
--login” flag is used to let Bash act as if it had been invoked as a login shell. The login shell reads certain initialization files (also known as interactive login shell files) from your home directory, like “
$ bash -l linuxtldr@linux:~$
Executing a Specific Command
If you use the “
-c” flag with the bash command, it will read and execute the first non-optional argument and ignore the remaining arguments.
$ bash -c "echo 'Executed by bash'" Executed by bash
Executing a Specific Script
To execute a specific script in Bash, specify its absolute or relative path to the bash command.
$ bash ./script.sh Script Executed
Executing a Specific Script While Printing Each Command Before Executing It
Use the “
-x” flag to print each command and their arguments in the script as they are executed.
$ bash -x ./script.sh + echo 'Script executed' Script executed
Executing a Specific Script and Stopping at the First Error
By default, when you execute a script with the bash shell, if any error occurs while executing, it will be printed and the rest of the script will continue to execute.
However, if your script has a bunch of things (functions, an array, if-else statements, etc.), a sudden error might interrupt the behavior of your script, and you might want to stop the script from executing further.
Use the “
-e” flag with the bash command to prevent your script from executing after the first error.
Bash example without flag:
$ bash ./script.sh Script executed ./script.sh: line 5: wrong-command: command not found Error Avoided
Bash example with flag:
$ bash -e ./script.sh Script executed ./script.sh: line 5: wrong-command: command not found
Starting Restricted Bash Shell
-r” or “
--restricted” flag will start the bash in restricted mode, which is more controlled than the standard shell. Overall, the behavior will be identical to bash except with a few restrictions.
- Changing directories with the cd command is not allowed.
- Executing shell scripts is not allowed.
- Setting or unsetting the PATH variable is not allowed.
- Importing functions is not allowed.
- Turning off restrictions using “
set +r” or “
set +o” is not allowed.
- Redirection signs “
>&“, and “
&>” are not allowed.
- Specifying file names with arguments like “
/” or “
–” is not allowed.
$ bash -r $ cd bash: cd: restricted
Printing Shell Input as It Is Typed
-v” flag will print all the typed commands on screen with the resulting output.
$ bash -v $ whoami whoami linuxtldr
Invoking Bash POSIX Mode
Using the “
--posix” flag will change the behavior of bash to act more closely to the POSIX standard and avoid the things that differ bash from the POSIX standard.
The following is a list of changes that occur when “POSIX mode” is enabled:
- The POSIX startup file will be executed rather than bash files.
- Bash will set the “
- Bash will search the “
$PATH” to find a new location if it is no longer in the hash table.
- Alias expansion is always enabled.
- The default history file is “
$ bash --posix
And here we will end the article.
If you have any suggestions related to this topic, feel free to tell us in the comment section.