Bash: Difference between revisions

From Chalphy Cyber Cavaliers
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:
The following is a list of the most common built in commands in bash with examples:
The following is a list of the most common built in commands in bash with examples:


*alias 'name'='command'
*alias
**<code>alias dir=ls</code>
**Will create a special note that says "hey, if this command is executed, run this command instead." The example will basically say if the command dir is typed, run ls instead.
**Will create a special note that says "hey, if this command is executed, run this command instead." The example will basically say if the command dir is typed, run ls instead.
**Example: <code>alias dir=ls</code>
*echo
**<code>echo "bash"</code>
**<code>echo "$PATH"</code>
**Prints out the text passed as an argument. Can be used in conjunction with variables to view them.
*cd
**<code>cd Desktop</code>
**Changes the working directory.
*export
**<code>export MYVAR="PASSME"</code>
**Sets variables to be passed to child processes of the shell itself.
*history
**<code>history</code>
**<code>history -c</code>
**Shows history of commands ran, use -c to clear it.
*source
**<code>source .bashrc</code>
**Restarts the shell using the file specified as the file that contains all the options. By default this is a file in your home directory named ".bashrc" and sourcing it again will restart the shell if you add anything to the file (like aliases).
*pwd
**prints the current directory.
*exit/logout
**The logout command only works if you are not running a terminal in a Linux UI. This does exactly what it sounds like and logs you out. The exit command does the same, but it works regardless of if you are running a terminal in the UI or running Linux with just a command line.

Latest revision as of 18:35, 21 March 2024

Bash (short for Bourne Again SHell) is the main scripting language used to provide a command line to Linux. I covered it briefly in Linux 101, but I will be covering it more in depth here. Very rarely will you write entire programs in bash, but it is a quick way to do many tasks all in one if you master it, it's just a little confusing as most lower level languages tend to be at times.

The most basic thing with bash, is that there is a variable that sets what directories to look for executable files. Every time you run a command, it searches these folders for a matching file to execute. These files can be anything, from python scripts to bash scripts to compiled executable files.

Bash builtins

The following is a list of the most common built in commands in bash with examples:

  • alias
    • alias dir=ls
    • Will create a special note that says "hey, if this command is executed, run this command instead." The example will basically say if the command dir is typed, run ls instead.
  • echo
    • echo "bash"
    • echo "$PATH"
    • Prints out the text passed as an argument. Can be used in conjunction with variables to view them.
  • cd
    • cd Desktop
    • Changes the working directory.
  • export
    • export MYVAR="PASSME"
    • Sets variables to be passed to child processes of the shell itself.
  • history
    • history
    • history -c
    • Shows history of commands ran, use -c to clear it.
  • source
    • source .bashrc
    • Restarts the shell using the file specified as the file that contains all the options. By default this is a file in your home directory named ".bashrc" and sourcing it again will restart the shell if you add anything to the file (like aliases).
  • pwd
    • prints the current directory.
  • exit/logout
    • The logout command only works if you are not running a terminal in a Linux UI. This does exactly what it sounds like and logs you out. The exit command does the same, but it works regardless of if you are running a terminal in the UI or running Linux with just a command line.