Storing lowercase conversion in a variable in BASH

时间:2018-02-05 12:58:46

标签: bash

I am trying to convert a string into its lowercase and store it in another variable so that I can conduct future operations on it.

month= $(echo "${1,,}")
echo $month

I also tried the following

month= "${1,,}"
echo $month

I get a command not found error such as : "bash: aug: command not found\r\n"

What am I doing wrong?

1 个答案:

答案 0 :(得分:3)

Bash is very particular about whitespace. Get rid of the space after the equal sign.

month=$(echo "${1,,}")

Or more directly:

month=${1,,}

When you have the space, Bash parses the line as two separate items:

month= $(echo "${1,,}")
^^^^^^ ^^^^^^^^^^^^^^^^
   |          |
   |          +-------> command
   +------------------> variable assignment

It tries to execute the result of $(echo "${1,,}") as if it were a command. That's why it complains about aug: command not found.

It thinks month= is a variable assignment of the form A=foo B=bar command, which runs command with $A and $B temporarily set to "foo" and "bar". When you use this feature the variable assignments are only in effect for the duration of the one command.