Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple double-dash(--) in one command line

Tags:

bash

for example

ssh root@me -- cat /etc/hostname -- cat /etc/hostname

I expect it output:

me
me

but it output

me
me
cat: cat: No such file or directory

I know the double-dash means ending parsing option, why it raise cat: cat: No such file or directory

like image 553
chikadance Avatar asked Dec 11 '22 17:12

chikadance


1 Answers

-- signals the end of option parsing. Nothing after -- will be treated as an option, even if it begins with a dash. As an example, ls -l will print a file listing in long format while ls -- -l looks for a file named -l.

ssh root@me -- cat /etc/hostname -- cat /etc/hostname

This sshes to a remote server and runs the command:

cat /etc/hostname -- cat /etc/hostname

That is a single cat command. Skipping over the --, it's equivalent to writing:

cat /etc/hostname cat /etc/hostname

It prints /etc/hostname, which is me. It then tries to print the file cat, which doesn't exist, giving the error cat: cat: No such file or directory. The program cat is complaining that the file cat doesn't exist. Then it prints /etc/hostname again.


If you want to execute multiple commands with ssh, do this:

ssh root@me 'cat /etc/hostname; cat /etc/hostname'

or this:

ssh root@me <<CMDS
cat /etc/hostname
cat /etc/hostname
CMDS
like image 145
John Kugelman Avatar answered Dec 29 '22 12:12

John Kugelman