Skip to Content
DocsLinuxBash Shell

Bash Shell

The Bash shell (Bourne Again SHell) is a widely used command-line interpreter for Unix-like operating systems. It provides a powerful interface for users to interact with the system, execute commands, and run scripts.

Identify the Bash Shell

To check which shell you’re currently using, you can run the following command in the terminal:

karchunt@kcserver:~$ echo $SHELL /bin/bash

You can also use the env command to see the current shell:

karchunt@kcserver:~$ env | grep SHELL SHELL=/bin/bash

Changing the Default Shell

If you want to change your default shell to Bash, you can use the chsh (change shell) command. Here’s how to do it:

karchunt@kcserver:~$ chsh Password: Changing the login shell for karchunt Enter the new value, or press ENTER for the default Login Shell [/bin/bash]: /bin/sh

or

karchunt@kcserver:~$ chsh -s /bin/bash Password:

After running the command, you will need to log out and log back in for the changes to take effect.

Bash Features

Command Auto-completion

Bash supports command auto-completion, which allows you to quickly complete commands and file names by pressing the Tab key. For example:

karchunt@kcserver:~$ ls folder folder1/ folder3/

Pressing Tab after typing folder will auto-complete the command to folder1/ or folder3/ if there is no ambiguity.

Command history

Bash maintains a history of commands you’ve executed, which you can access using the history command or by pressing the Up and Down arrow keys. This feature allows you to easily recall and re-execute previous commands.

karchunt@kcserver:~$ history 1 ls 2 cd folder1 3 nano file.txt 4 history

Aliases

Bash allows you to create aliases for frequently used commands, making it easier to execute them. You can define an alias using the alias command:

karchunt@kcserver:~$ alias ll='ls -la' karchunt@kcserver:~$ ll

If you want to see all defined aliases, simply type alias without any arguments:

karchunt@DESKTOP-CCAQ09F:~$ alias alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -la' alias ls='ls --color=auto'

To make it permanent, you can add the alias definition to your ~/.bashrc file.

Customizing Bash Prompt

Normally, when you login to a Bash shell, you will see ~$ as your prompt. You can customize the prompt by modifying the PS1 variable in your ~/.bashrc file. You can check the current setting of PS1 by running:

karchunt@kcserver:~$ echo $PS1 \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

Here are some common escape sequences you can use to customize your prompt:

  • \u: Username
  • \h: Hostname
  • \w: Current working directory
  • \d: Date
  • \t: Current time

To change your prompt, you can edit the PS1 variable in your ~/.bashrc file or set it directly in the terminal:

karchunt@kcserver:~$ PS1="\d \u@\h:\w\$" Thu Jan 29 karchunt@kcserver:~$
Last updated on