So I have this function in bash that receives as first parameter an empty string variable and as second parameter a character,

function check_cases {
    case $2 in
        r) $1=$(echo "$1 Read");;
        w) $1=$(echo "$1 Write");;
        x) $1=$(echo "$1 Execution");;

I've tried all different ways to concatenate the string variable but it always says that

=: command not found

about the = operator. I create and empty local string variable inside a function like

local var=""

And pass it with a character variable

check_cases "$var" "$char"

I want to understand what's going on and how to solve this problem.


It looks like you're trying to do a pass by reference type function.

These aren't really a good fit for for bash scripting. We can fake it with the eval command, but this has a lot of risky behaviour (potential evaluation of commands embedded in strings) and so isn't recommend.

In this example, the code would look something like:

function check_cases {
    case $2 in
        r) eval $1=\"\$$1 Read\" ;;
        w) eval $1=\"\$$1 Write\" ;;
        x) eval $1=\"\$$1 Execution\" ;;

check_cases var r
check_cases var w
check_cases var x

echo $var

Instead it's normally better to use pass by value semantics; just return the value you want and have the caller append it to the variable.


function check_cases {
    case $1 in
        r) echo " Read" ;;
        w) echo " Write" ;;
        x) echo " Execution" ;;

var="$var$(check_cases r)"
var="$var$(check_cases w)"
var="$var$(check_cases x)"

echo $var
  • Thank you very much, I realized that i was programming in bash using C or Java logic, your answer worked perfectly – Baratao00 Aug 28 '16 at 16:14

It's not clear what you want to achieve, but anyhow...

First of all, the syntax for value assignment is not






won't work, because the variable name can't begin with a number.

To assign values to positional parameters, do this:

function check_cases {
    case $2 in
        r) append=' Read';;
        w) append=' Write';;
        x) append=' Execution';;
    set "$1""$append" "$2" # notice no space between "$1" and "$append"
    echo "$@" # print all parameters
    echo Number of parameters: $#

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.