Bash Commands: A Comprehensive Guide to Essential Shell Commands

set: Sets or unsets shell options and positional parameters.                            

echo

Outputs the given string(s) to the terminal.               
set
– “arg1” “arg2” # Sets positional parameters                                            echo “Hello, World!” # Prints “Hello, World!” to the terminal        

Chmod


Changes the file mode (permissions) of a file.                                    

./script_file

Executes a script located in the current directory.

chmod u+x script_file # Adds execute permission for the user                           ./script_file # Runs the script named ‘script_file’                   

let:
Performs arithmetic operations in shell scripts.                                       
let result=5+3 # Sets ‘result’ to 8                                       

let sum:
A specific use of let to calculate a sum.                                       
let sum=1+2 # Sets ‘sum’ to 3

expr: Evaluates expressions and performs arithmetic operations.
expr
2 + 3 # Outputs 5

bc:
An arbitrary precision calculator language.
echo “scale=2; 3/2″ | bc # Outputs 1.50

scale:
Sets the number of decimal places in bc.
scale=2 # Sets decimal places to 2 in ‘bc’

quit:
Exits a program, such as bc.
quit # Exits ‘bc’

Export PATH=$PATH:/newDir:


Adds a directory to the PATH environment variable. # Adds ‘/newDir’ to PATH

export PATH=$PATH:
: Includes the current directory in your PATH. # Adds current directory to PATH

Export PATH=$PATH:$HOME


Adds your home directory to the PATH. # Adds home directory to PATH

\a:


An escape sequence that produces an audible or visible bell.

\d:


The date in “Weekday Month Date” format.

\h:


The hostname up to the first ..

\H:


The full hostname.

\j:


The number of jobs currently managed by the shell.

\l:


The basename of the shell’s terminal device name.

\n:


A newline.

\s:


The name of the shell.

\u:


The username of the current user.

\v:


The version of bash.

\w:


The current working directory.

\W:


The basename of the current working directory.


!:

The history number of this command.

#:


The command number of this command.

$:


If the effective UID is 0, a #, otherwise a $.

\nnn:


The character corresponding to the octal number nnn.

\:


A backslash.

[:


Begin a sequence of non-printing characters.

]:


End a sequence of non-printing characters.

\t:


The current time in 24-hour HH:MM:SS format.

$PWD:


The current working directory.

string:
Any sequence of characters.


-n string:

True if the string is not null.


-z string:

True if the string is null (zero length).

String1 = string2:


True if the strings are equal.

String1 != string2:


True if the strings are not equal.

Int1 relop int:


Integer comparison, where relop is -eq, -ne, -lt, -le, -gt, or -ge.


-a:

Logical AND.


-o:

Logical OR.


!:

Logical NOT.



-d file:

Checks if file exists and is a directory.

if [ -d “$directory” ]; then
  echo “The directory exists.”
else
  echo “The directory does not exist.

fi


-f file:

Checks if file exists and is a regular file (not a directory or device).

if [ -f “$file” ]; then
  echo “The file exists.”
else
  echo “The file does not exist.”
fi


-L file:

Checks if file exists and is a symbolic link.

if [ -L “$link” ]; then
  echo “The symbolic link exists.”
else
  echo “The symbolic link does not exist.”
fi


-r file:

Checks if file exists and is readable.

if [ -r “$file” ]; then
  echo “The file is readable.”
else
  echo “The file is not readable.”
fi


-s file:

Checks if file exists and has a size greater than zero.

if [ -s “$file” ]; then
  echo “The file has content.”
else
  echo “The file is empty.”
fi

-w file: Checks if file exists and is writable.

if [ -w “$file” ]; then
  echo “The file is writable.”
else
  echo “The file is not writable.”
fi

-x file: Checks if file exists and is executable.

if [ -x “$file” ]; then
  echo “The file is executable.”
else
  echo “The file is not executable.”
fi

-d file: Checks if file exists and is a directory.

if [ -d “$directory” ]; then
  echo “The directory exists.”
else
  echo “The directory does not exist.”
fi

-f file: Checks if file exists and is a regular file (not a directory or device).

if [ -f “$file” ]; then
  echo “The file exists.”
else
  echo “The file does not exist.”
fi

-L file: Checks if file exists and is a symbolic link.

if [ -L “$link” ]; then
  echo “The symbolic link exists.”
else
  echo “The symbolic link does not exist.”
fi

-r file: Checks if file exists and is readable.

if [ -r “$file” ]; then
  echo “The file is readable.”
else
  echo “The file is not readable.”
fi

-s file: Checks if file exists and has a size greater than zero.

if [ -s “$file” ]; then
  echo “The file has content.”errda
else
  echo “The file is empty.”
fi

-w file: Checks if file exists and is writable.

if [ -w “$file” ]; then
  echo “The file is writable.”
else
  echo “The file is not writable.”
fi

-x file: Checks if file exists and is executable.

if [ -x “$file” ]; then
  echo “The file is executable.”
else
  echo “The file is not executable.”
fi