How to assign a parameter value to a variable on UNIX?

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

This does not work as I expected:

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

any thoughts?

+3
source share
1 answer

Remove set.

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

Or if you want to install it so that it is available for subsequent programs running in the shell:

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

The keyword is exportdescribed pretty well here .

+8
source

Source: https://habr.com/ru/post/1742591/


All Articles