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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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
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. |