Beginners Guide for Bash Command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

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.

Tutorial Details

DescriptionBash (Bourne Again Shell)
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesbash
Internet RequiredNo

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.

$ bash

If your default shell is bash, then you will find multiple bash processes running on your system.

$ ps

Output:

Running multiple bash processes with bash command

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 (“~/.bashrc“).

$ bash --norc

Output:

Starting shell session using bash command without user configuration

Invoked Bash as the Login Shell

The “-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_profile“.

$ bash -l

Output:

Launching bash session as login shell

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'"

Output:

Executing command with bash command

Executing a Specific Script

To execute a specific script in Bash, specify its absolute or relative path to the bash command.

$ bash ./script.sh 

Output:

Running script using bash command

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 

Output:

Running script using bash command but first printing each command from script before executing

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 

Output:

Script executed avoiding error using bash command

Bash example with flag:

$ bash -e ./script.sh

Output:

Script stopped executing after first error with bash command

Starting Restricted Bash Shell

The “-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

Output:

Starting restricted bash shell and running cd command that is restricted

Printing Shell Input as It Is Typed

The “-v” flag will print all the typed commands on screen with the resulting output.

$ bash -v
$ whoami

Output:

Printing all typed command in bash verbose mode

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 “POSIXLY_CORRECT” variable.
  • 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 “~/.sh_history“.
$ bash --posix

Output:

Starting bash with posix standard

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.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.