Skip to main content
Engineering LibreTexts

01-B.15: Bash Command How To

  • Page ID
    26791
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Enter Commands in Bash

    Just like a programming language the bash shell has a syntax — that is the manner in which a command is entered on the keyboard. If commands, options and arguments are not entered in the proper manner the user will receive an error. Most commands have a variety of options that may or may not be optional. Take the ls command that is shown in the example on the left. This command will allow the user to simply enter the command without any options or arguments; it simply assumes the user is asking for a short list of the current directory. (a short list is simply the file/directory names). If you enter options, it still assumes those options pertain to the listing of the current directory. Only if the user enters one or more arguments does it list something other then the current directory. So, how do you know which commands require options, or arguments? It simply takes practice and actually working on a Linux system.

    If you fail to enter a command in the proper syntax, the shell will return an error message of some type. These messages are not always as easy to decipher as we would like, but often they are pretty straight forward. The most common one is probably "command not found", which is the result of a typo on the command line. Another common error is "no such file or directory", again, often caused by a typo, or perhaps an incorrect path name.

    In the following example, the user enters a command, followed by an option, and then the argument(s). Options are always proceeded with a dash in Linux. You can string the options together using just a single dash. Be aware, that you do not need to use options, or arguments on many commands, but on other commands arguments are required. In this command we see the user enters the ls command, which provides a list of the files in the specified directory. The user also provides 2 options: 1) the a option says to show all the files, even the hidden ones; and 2) the l option which provides a long listing, giving all the file details for each file. The last part of the command is the argument, in this case the user enters /usr, specifying the system should list the files in the /usr directory. Again — time and experience will help you with all of this.

    pbmac@pbmac-server $ ls —al /usr 
    total 196
    drwxr-xr-x  12 root root   274 Dec  7  2020 .
    drwxrwxrwx  21 root root   550 May 24 17:45 ..
    dr-xr-xr-x   2 root root 59808 Dec 26  2020 bin
    drwxr-xr-x   2 root root    37 Aug 12  2020 games
    drwxr-xr-x  98 root root  8023 Dec 19  2020 include
    dr-xr-xr-x  40 root root  1214 Dec 19  2020 lib
    dr-xr-xr-x 106 root root 70319 Dec 19  2020 lib64
    drwxr-xr-x  40 root root  2771 Dec 19  2020 libexec
    drwxr-xr-x  12 root root   252 Dec  7  2020 local
    dr-xr-xr-x   2 root root 16054 Dec 26  2020 sbin
    drwxr-xr-x 197 root root  4832 Dec 31  1969 share
    drwxr-xr-x   4 root root    83 Dec  7  2020 src
    lrwxrwxrwx   1 root root    10 Aug 12  2020 tmp -> ../var/tmp
    
    Figure \(\PageIndex{1}\): Command line: options and arguments ("Linux Shell" by Patrick McClanahan is licensed under CC BY—SA 4.0)

    This table provides some examples of commands, commands with options, commands with arguments, and commands with options and arguments. Any of these combinations are valid. 

    Command line entered Explanation Does what?
    ls Command only — no options, no arguments. Returns a list of the files/directory names from the current directory.
    ls —al Command with 2 options. Linux allows you to enter: ls —a —l, or combine the options in one: ls —al. The —a shows the hidden files — those files that begin with a period — in the example you use a file called . and one called .. The —l is a "long" listing that lists the details of each file/directory, as shown in the example above.
    ls /usr Command with an argument but no options. Linux knows this is an argument because it is NOT preceded by a dash. The output is the default listing of the /usr directory — which is simply a listing of the files/directories in the /usr directory.
    ls —al /usr Command with both options and arguments. This produces a long listing of all files/directories — including hidden files/directories — in the /usr directory.

     


    This page titled 01-B.15: Bash Command How To is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?