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

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.

主站蜘蛛池模板: 敦煌市| 龙州县| 贵溪市| 凌源市| 潜江市| 崇左市| 海安县| 澜沧| 龙泉市| 高雄市| 平邑县| 旌德县| 东莞市| 泰顺县| 巩留县| 治多县| 竹山县| 浙江省| 错那县| 通州市| 宁南县| 武威市| 阜平县| 青冈县| 佳木斯市| 荣成市| 资溪县| 云安县| 平南县| 云安县| 庆安县| 和龙市| 南康市| 兴城市| 东港市| 乌兰浩特市| 青海省| 海原县| 白银市| 浠水县| 马鞍山市|