Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you assign the value of a parameter to a variable in UNIX?

#!/bin/bash
if [ ! $1 ]
then
 echo "no param"
else
    set FAV_COLOR=$1
    echo "My fav color is ${FAV_COLOR}"
fi

This is not working how I expected:

>favcol.sh blue
My fav color is FAV_COLOR=blue

Any thoughts?

like image 564
qodeninja Avatar asked Jan 23 '23 04:01

qodeninja


1 Answers

Remove set.

FAV_COLOR=$1
echo "My fav color is ${FAV_COLOR}"

Or if you want to set it so that it is available to subsequent programs run in the shell:

export FAV_COLOR=$1
echo "My fav color is ${FAV_COLOR}"

The export keyword is described fairly well here.

like image 57
Stephen Avatar answered Jan 29 '23 07:01

Stephen