1

I have written a 'to_upper' function using bash scripting:

to_upper() {

    local string=$1
    echo $string | tr "[:lower:]" "[:upper:]"
    return 0
}

However, the output of:

VAL=bla
echo $(to_upper bla)
echo $(to_upper $VAL)

is

BLA
1

Does anyone know what is going on here?

NOTE: It seems my example does not reproduce my error. However, what I do have is this situation:

DEVICE_STATUS=$(get_device_status)
echo $DEVICE_STATUS $(to_upper $DEVICE_STATUS)

The output is then:

active 1

My example seems to not reproduce the problem, but it's there on my script.

NOTE 2: I fixed the problem; it was some grep output on the get_device_status function which ended up on $DEVICE_STATUS.

7
  • Not reproducible in my bash 4.1.5 too. Commented Nov 14, 2012 at 9:43
  • neither in my bash, GNU bash, version 4.2.37(2)-release (x86_64-unknown-linux-gnu) Commented Nov 14, 2012 at 9:44
  • 1
    You don't need to call the function in sub-process. Instead of "echo $(to_upper bla)" you can go "to_upper bla". Commented Nov 14, 2012 at 9:44
  • Please tell us what shell and version are you using (and perhaps also platform). Commented Nov 14, 2012 at 9:45
  • To all: please see my edit (I'm on ubuntu 12.04, bash 4.2.24) Commented Nov 14, 2012 at 9:46

1 Answer 1

1

I solved my own problem: in the get_device_status function there was a grep command which was outputting junk to $DEVICE_STATUS. Redirecting the grep output to /dev/null solved the problem.

I am not quite sure in this case why $DEVICE_STATUS picked up the correct value on the echo but not on the call to to_upper.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.