15 Special Characters You Need to Know for Bash

Bash is a powerful shell used in Unix-based operating systems like Linux and macOS. It offers a wide range of commands and features, including the use of special characters to perform specific actions or modify commands. Here are 15 special characters you need to know for Bash:

  1. $ – The dollar sign is used to indicate a variable in Bash. You can use it to define a variable, access a variable’s value, or substitute the value of a variable into a command.
  2. ~ – The tilde is used to refer to the home directory of the current user. For example, ‘cd ~’ will take you to your home directory.
      • The asterisk is used as a wildcard to represent any number of characters in a filename or path. For example, ‘rm *.txt’ will remove all files with a .txt extension in the current directory.
  3. ? – The question mark is used as a wildcard to represent a single character in a filename or path. For example, ‘ls ???.txt’ will show all files in the current directory that have a three-letter extension ending in .txt.
  4. – The square brackets are used to represent a range of characters in a filename or path. For example, ‘ls [a-z]*’ will show all files in the current directory that start with a lowercase letter.
  5. {} – The curly braces are used to create a list of options in a command. For example, ‘cp file{1,2,3}.txt directory/’ will copy files 1, 2, and 3 with a .txt extension to the directory.
  6. & – The ampersand is used to run a command in the background, allowing you to continue using the terminal while the command runs.
  7. | – The pipe is used to connect the output of one command to the input of another command. For example, ‘ls | grep file’ will show all files in the current directory that contain the word ‘file’.
  8. ; – The semicolon is used to separate multiple commands on a single line. For example, ‘cd directory; ls’ will change to the directory and then list its contents.
  9. \ – The backslash is used to escape special characters, allowing them to be used as regular characters in a command. For example, ‘echo $HOME’ will show the value of the HOME variable, while ‘echo $HOME’ will show ‘$HOME’.
  10. ‘ – The single quote is used to enclose a string of text, preserving all special characters within it. For example, ‘echo ‘$HOME’’ will show ‘$HOME’.
  11. “ – The double quote is used to enclose a string of text, allowing variables and other special characters to be interpreted within it. For example, ‘echo “$HOME”’ will show the value of the HOME variable.
  12. – The hash symbol is used to represent a comment in a Bash script. Everything after the # on a line is ignored by the shell.
  13. () – The parentheses are used to group commands together, allowing them to be treated as a single command. For example, ‘(cd directory && ls)’ will change to the directory and list its contents.
  14. ! – The exclamation mark is used for history expansion, allowing you to repeat previous commands or modify them. For example, ‘!!’ will repeat the previous command, and ‘!$’ will substitute the last argument of the previous command into the current command.