1.11 Specifica degli argomenti

Dopo il comando e una delle sue opzioni, molti comandi accettano uno o più argomenti. I comandi che utilizzano argomenti possono richiedere uno o più di essi. Ad esempio, il comando touch mostrato nella pagina precedente richiede almeno un argomento per specificare il nome file su cui agire.

Il comando ls, d’altra parte, consente di specificare l’argomento del nome file, ma non è stato richiesto. Alcuni comandi come il comando cp (copia file) e il comando mv (sposta file) richiedono almeno due argomenti, il file sorgente e quello di destinazione.

Gli argomenti che contengono caratteri insoliti come spazi o caratteri non alfanumerici dovranno di solito essere citati, racchiudendoli tra virgolette doppie o virgolette singole. Le virgolette doppie impediranno alla shell di interpretare alcuni di questi caratteri speciali; le virgolette singole impediscono alla shell di interpretare caratteri speciali.

Nella maggior parte dei casi, le virgolette singole sono “più sicure” e dovrebbero probabilmente essere utilizzate ogni volta che si ha un argomento che contiene caratteri non alfanumerici. Citazioni e caratteri speciali saranno trattati in maggior dettaglio in una sezione successiva, ma se vuoi avere un’idea di quanto siano importanti, dai un’occhiata all’esempio nella sezione Considerare questo.

Consider This

To understand the importance of quotes, consider a simple scenario in which you want to go to your home directory (which can be accomplished with the cd command) and execute the echo command to display the string “hello world!!” on the screen. The echo command displays text to the terminal.

You might first try the echo command without any quotes, unfortunately without success:

sysadmin@localhost:~$ cd
sysadmin@localhost:~$ echo hello world!!
echo hello worldcd
hello worldcd

Using no quotes failed because the shell interprets the !! characters as special shell characters; in this case they mean “replace the !! with the last command that was executed”. In this case, the last command was the cd command, so cd replaced !! and then the echo command displayed hello worldcd to the screen.

You may want to try the double quotes to see if they will block the interpretation (or expansion) of the !! characters. The double quotes block the expansion of some special characters, but not all of them. Unfortunately, double quotes do not block the expansion of !!:

sysadmin@localhost:~$ cd
sysadmin@localhost:~$ echo “hello world!!”
echo “hello worldcd”
hello worldcd
Using double quotes preserves the literal value of all characters that they enclose except the $ (dollar sign), ` (backquote), \ (backslash) and ! (exclamation point).

When you enclose text within the ‘ (single quote) characters, then all characters have their literal meaning:

sysadmin@localhost:~$ cd
sysadmin@localhost:~$ echo ‘hello world!!’
hello world!!