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.
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.


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


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


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


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


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 ./ 


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 ./ 


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 executed avoiding error using bash command

Bash example with flag:

$ bash -e ./


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


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


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


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.