Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash set variable from commandline argument

Tags:

bash

shell

cat ./1.sh

#!/bin/bash
echo $1
set var1 = $1
echo var1 is $var1

kostas@elem:~/1$ argument1 var1 is

How to set var1 from first commandline argument?

like image 523
user3116685 Avatar asked Dec 18 '13 21:12

user3116685


1 Answers

The correct assignment is simply the following, with no spaces on either side of the equal sign:

var1=$1

The command set var1 = $1 actually does the following:

  1. Sets the value of $1 to "var1"
  2. Sets the value of $2 to "="
  3. Sets the value of $3 to the original first parameter $1.
like image 50
chepner Avatar answered Sep 30 '22 08:09

chepner