LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why set command change $1's value? (https://www.linuxquestions.org/questions/programming-9/why-set-command-change-%241s-value-4175597074/)

utnubudnai 01-09-2017 02:42 AM

why set command change $1's value?
 
Code:

cat a.sh
Code:

#!/bin/bash
echo $*
set a=b
echo $*

Code:

bash a.sh 1 2 3 4 5
and the output is
1 2 3 4 5
a=b

why command
Code:

set a=b
change $1's value?

astrogeek 01-09-2017 02:56 AM

Because that is exactly what the set command is supposed to do. What were you expecting it to do?

See man bash, look under Shell Builtins, set for full details, but here is the applicable part:

Code:

set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
      ...Any arguments remaining  after  option  processing  are treated as values for the positional parameters...

And welcome to LQ

makyo 01-19-2017 10:14 AM

Hi.

In the off-chance you have been coding/inherited csh, one does use:
Code:

set a=b
for assignments, but in Bourne shell relatives, one should use:
Code:

a=b
Compare man pages for csh/tcsh with man pages for sh/bash/ksh.

Best wishes ... cheers, makyo

utnubudnai 06-06-2017 07:55 AM

Thanks astrogeek, and makyo, your replies are very useful for me, happy to know you guys:)

BW-userx 06-06-2017 08:26 AM

look into 'shift' to move the value over to the next one.

The shift built-in

Handling positional parameters
excerpt
Code:

Shifting

The builtin command shift is used to change the positional parameter values:

    $1 will be discarded
    $2 will become $1
    $3 will become $2
    …
    in general: $N will become $N-1



All times are GMT -5. The time now is 09:42 AM.