官术网_书友最值得收藏!

Passing command arguments

So far, we ran the ls command only on the current working directory. However, you can list the contents of any directory without having to change to it. For example, if your current working directory is /home/elliot:

elliot@ubuntu-linux:~$ pwd
/home/elliot

You can list all the files in /home/angela by running the ls -a /home/angela command:

elliot@ubuntu-linux:~$ ls -a /home/angela
. .. .bash_history .bash_logout .bashrc Music .profile
elliot@ubuntu-linux:~$ pwd

/home/elliot
elliot@ubuntu

I was able to list the contents of /home/angela while still being in /home/elliot. This is possible because the ls command accepts any file as an argument.

WHAT IS AN ARGUMENT?

An argument, also called a command-line argument, is simply any filename or data that is provided to a command as an input.

Figure 11: Linux Command Structure

You can see in the preceding image the general structure of a Linux command.

In Linux terminology, we use the verb pass when talking about command options and arguments. To use the correct Linux terminology, for example, in the preceding image, we say, "We passed the /home/angela directory as an argument to the ls command."

You will often find Linux users very keen on using the right terminology. Moreover, using the proper terminology can help you pass a job interview and land your dream job!

Notice in the preceding figure, we used the plural nouns options and arguments. That's because some commands can accept multiple options and arguments.

For example, we can do a long listing for all the files in /home/angela by running the ls -a -l /home/angela command:

elliot@ubuntu-linux:~$ ls -a -l /home/angela 
total 28
drwxr-xr-x 3 angela angela 4096 Jan 20 13:43 .
drwxr-xr-x 9 root root 4096 Jan 17 04:37 ..
-rw------- 1 angela angela 90 Jan 20 13:43 .bash_history
-rw-r--r-- 1 angela angela 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 angela angela 3771 Apr 4 2018 .bashrc
drwxrwxr-x 2 angela angela 4096 Jan 19 19:42 Music
-rw-r--r-- 1 angela angela 807 Apr 4 2018 .profile

So now you see a long listing of all the files in /home/angela including the hidden files, also notice that the ordering of the options doesn't matter here, so if you run the ls -l -a /home/angela command:

elliot@ubuntu-linux:~$ ls -l -a /home/angela 
total 28
drwxr-xr-x 3 angela angela 4096 Jan 20 13:43 .
drwxr-xr-x 9 root root 4096 Jan 17 04:37 ..
-rw------- 1 angela angela 90 Jan 20 13:43 .bash_history
-rw-r--r-- 1 angela angela 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 angela angela 3771 Apr 4 2018 .bashrc
drwxrwxr-x 2 angela angela 4096 Jan 19 19:42 Music
-rw-r--r-- 1 angela angela 807 Apr 4 2018 .profile

You will get the same result. This was an example of passing two commands options, what about passing two arguments? Well, you can do a long listing for all the files in /home/angela and /home/elliot at the same time by passing /home/elliot as a second argument:

elliot@ubuntu-linux:~$ ls -l -a /home/angela /home/elliot
/home/angela:

total 28
drwxr-xr-x 3 angela angela 4096 Jan 20 13:43 .
drwxr-xr-x 9 root root 4096 Jan 17 04:37 ..
-rw------- 1 angela angela 90 Jan 20 13:43 .bash_history
-rw-r--r-- 1 angela angela 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 angela angela 3771 Apr 4 2018 .bashrc
drwxrwxr-x 2 angela angela 4096 Jan 19 19:42 Music
-rw-r--r-- 1 angela angela 807 Apr 4 2018 .profile

/home/elliot:
total 28
drwxr-xr-x 3 elliot elliot 4096 Jan 20 16:26 .
drwxr-xr-x 9 root root 4096 Jan 17 04:37 ..
-rw------- 1 elliot elliot 90 Jan 20 13:43 .bash_history
-rw-r--r-- 1 elliot elliot 220 Dec 26 23:47 .bash_logout
-rw-r--r-- 1 elliot elliot 3771 Dec 26 23:47 .bashrc
drwxr-xr-x 2 elliot elliot 4096 Jan 19 14:20 Desktop
-rw-r--r-- 1 elliot elliot 807 Apr 4 2018 .profile

So now, you can see the contents of both the /home/elliot and /home/angela directories at the same time.

主站蜘蛛池模板: 凤山市| 临漳县| 柳州市| 沙坪坝区| 舒城县| 平塘县| 临清市| 乾安县| 神木县| 海南省| 常山县| 柯坪县| 许昌县| 固原市| 洪湖市| 苏尼特右旗| 出国| 黑水县| 兴隆县| 佛山市| 尚志市| 黄骅市| 团风县| 桑日县| 苏尼特右旗| 黄陵县| 客服| 万全县| 阜平县| 建昌县| 五大连池市| 获嘉县| 章丘市| 百色市| 鹰潭市| 长汀县| 林芝县| 西畴县| 贺州市| 丰县| 高台县|