1

According to this answer ...

A command is split into an array of strings named arguments. Argument 0 is (normally) the command name, argument 1, the first element following the command, and so on.

$ ls -la /tmp /var/tmp
arg0 = ls
arg1 = -la
arg2 = /tmp
arg3 = /var/tmp

Would it be possible to print out each argument ... let say with echo.

I've been using echo $@ and echo $0 $1 $2 $3 right after executing ls -la /tmp /var/tmp command but didn't really work.

I also tried this without > /dev/null but didn't work as well. (I'm getting exactly similar output as below)

user@linux:~$ ls -la /tmp /var/tmp > /dev/null 
user@linux:~$ echo $@

user@linux:~$ 

user@linux:~$ ls -la /tmp /var/tmp > /dev/null 
user@linux:~$ echo $0
bash
user@linux:~$ 

user@linux:~$ ls -la /tmp /var/tmp > /dev/null 
user@linux:~$ echo $1

user@linux:~$ 

user@linux:~$ ls -la /tmp /var/tmp > /dev/null 
user@linux:~$ echo $0 $1 $2 $3
bash
user@linux:~$ 

Desired Output

I'm expecting to see the following output on my terminal screen after executing ls -la /tmp /var/tmp

Input

ls -la /tmp /var/tmp

Output

arg0 = ls
arg1 = -la
arg2 = /tmp
arg3 = /var/tmp
  • There's a misunderstanding in the middle here; you start by saying and showing the breakdown of ls -la /tmp /var/tmp -- the command and arguments -- but then proceed to show your interactive shell (with its echo commands) expecting to see similar argument expansions. But your only commands are the ones you're executing. – Jeff Schaller Jul 04 '19 at 01:19
  • Thanks @JeffSchaller for the comment. I've updated my post –  Jul 04 '19 at 01:21
  • 2
    What problem are you trying to solve here? You execute a command and the $0 $1, etc arguments exist for that command, but then the command exits, and those parameters are gone. Are you trying to wrap a command and check arguments? Debug something? I'm not sure how to answer in a way that's useful. – Jeff Schaller Jul 04 '19 at 01:22
  • Thanks @Jeff, I'm just trying to understand previous answer (ls -la /tmp /var/tmp) by producing something visible on my screen with echo.

    e.g. arg0 is ls, arg1 is -la and so on

    –  Jul 04 '19 at 01:29
  • 1
    Since your shell has no arguments, maybe try setting some or writing a script and running it with arguments. – muru Jul 04 '19 at 01:34
  • In shells that provide interactive command history, you may be able to recover the arguments after the fact using the appropriate event and word designators e.g. (in bash) echo !:1, echo !:2 etc. – steeldriver Jul 04 '19 at 01:41
  • @muru, that was the plan. Do you have anything in mind? Btw, I've updated my question to make it clearer (I hope it does) –  Jul 04 '19 at 01:41
  • 1
    Put what you have in a script and see what goes wrong. – muru Jul 04 '19 at 01:41
  • This was marked as a duplicate because it really seems to be asking the same thing as your newer question. Please let me know if you feel it is not and is actually a different question. That said, I think you might just be looking for set -x. Run set -x and then ls -la /tmp /var/tmp and you will see the ls command split into its arguments as you expect. Run set +x to remove this debugging output. – terdon Jul 04 '19 at 14:55
  • But here Sabrina is just testing that answer she cites. Jeff points out 16 h ago about the "misunderstanding in the middle". I answered 12 h ago to show what he means. The newer script Q with the "seq" suddenly opens the Q of how to loop and format the args. The interesting effects of $0 that I show are not touched --> not a duplicate –  Jul 04 '19 at 17:32

1 Answers1

0

The echo $0 did output "bash" in your example. This is true, because bash IS running when you are in the shell.

The arguments you want to access are given to the command ls. When that returns, and your prompt, the arguments are gone.

After you create a file (a "script") containing the line

echo $0 $1 $2

and call it by sourcing:

. myecho.cmd a b c d 

...you get:

bash a b

After you make it an executable (add #! first line, chmod, cp to a path dir) you call it directly:

myecho.cmd a b c d

and now $0 is the name of the new command:

/usr/local/bin/myecho.cmd a b 

(I call it .cmd just so it sticks out. just don't call it "echo")