12

I do a echo $1, it prints out what is the default login shell used.

But for echo $2 onwards, all I get is a newline. Why is that?

3 Answers 3

23

$1 (or $2,$3 ...) is supposed to be the arguments given to some script.

Here's an example script:

#!/bin/bash

echo "\$1 is now $1"
echo "\$2 is now $2"
echo "\$3 is now $3"

And the example output

jaba@lappy:/tmp$ ./example.sh 
$1 is now 
$2 is now 
$3 is now 
jaba@lappy:/tmp$ ./example.sh 1 2 3
$1 is now 1
$2 is now 2
$3 is now 3
1
  • 1
    And $0 is the name of the scipt being executed, analogous to argv. Jul 30, 2010 at 9:29
4

$1 is the argument passed for shell script.

Suppose, you run

./myscript.sh hello 123 

then

$1 will be hello

$2 will be 123

3

In your case $1 prints default login shell used because this argument was passed to script that runs your login shell. But if you'll write and run your own script in current session, $1, $2, ... will be parameters that you send to your script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.