help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] View Positional Parameters of Commands


From: Clark Wang
Subject: Re: [Help-bash] View Positional Parameters of Commands
Date: Tue, 28 Jul 2015 23:18:37 +0800

On Tue, Jul 28, 2015 at 11:12 PM, Clark Wang <address@hidden> wrote:

> On Tue, Jul 28, 2015 at 10:58 PM, Michael Convey <address@hidden>
> wrote:
>
>> I'm trying to understand positional parameters. If I run the the following
>> command:
>>
>> ls -al
>>
>
> $*N* is a shell terminology. It has nothing to do with ls.
>
>>
>> echo $0 should equal ls
>> echo $1 should equal -a
>> echo $2 should equal -l
>>
>> However, no matter which command I run, echo $0 always equals "bash" and
>> $1, $2, etc. are empty. I suspect this is because my original command is
>> being run in a subshell and the echo commands are being run in a different
>> subshell, which is invoked by parent shell as the 'bash' command. So, I
>> tried to run these commands using 'source command', to run everything in
>> the parent shell. However, I get the following error:
>>
>> bash: source: /usr/bin/command: cannot execute binary file
>>
>> So I tried the following:
>>
>> ls -al; echo $0; echo $1
>>
>> But, I get the same results. What is the best way to do what I'm trying to
>> accomplish -- view positional parameters of commands I run?
>>
>
> For example:
>
>   $ foo() { echo $0 $1 $2; }
>   $ foo
>   /Users/clarkw/utils/bin/bash
>   $ foo -a
>   /Users/clarkw/utils/bin/bash -a
>   $ foo -al
>   /Users/clarkw/utils/bin/bash -al
>   $ foo -al bar
>   /Users/clarkw/utils/bin/bash -al bar
>   $
>

Or you can change current shell's $*N* like this:

  # echo "<$0> <$1> <$2>"
  </Users/clarkw/utils/bin/bash> <> <>
  # set -- -a
  # echo "<$0> <$1> <$2>"
  </Users/clarkw/utils/bin/bash> <-a> <>
  # set -- -al
  # echo "<$0> <$1> <$2>"
  </Users/clarkw/utils/bin/bash> <-al> <>
  # set -- -al bar
  # echo "<$0> <$1> <$2>"
  </Users/clarkw/utils/bin/bash> <-al> <bar>
  #


reply via email to

[Prev in Thread] Current Thread [Next in Thread]