On a Linux shell, what is echo $1 supposed to do?

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

$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 is the argument passed for shell script.

Suppose, you run

./myscript.sh hello 123 

then

$1 will be hello

$2 will be 123

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like