Setting a variable in a one-liner Bash script Setting a variable in a one-liner Bash script bash bash

Setting a variable in a one-liner Bash script


You need double-semicolons ;; to separate the clauses of a case statement, whether it is one line or many. You also have to be careful with { … } because it is used for I/O redirection. Further, both { and } must be tokens at the start of a command; there must be a space (or newline) after { and a semicolon (or ampersand, or newline) before }. Even with that changed, the assignment would not execute the code in between the braces. For that, you could use command substitution:

export class=$(read -p "What Is Your Profession?" a; case $a in "Theif") echo "Stealth" ;; "Cleric") echo "Heals?" ;; "Monk") echo "Focus?" ;; *) echo "invalid choice $a";; esac)

Finally, you've not run a spell-check on 'Thief'.


Here is one-liner you can use:

export class=`{ read -p "What Is Your Profession? " a; case $a in "Theif") echo "Stealth";; "Cleric") echo "Heals?";; "Monk") echo "Focus?";; *) echo "invalid choice" a;; esac; }`

OR better you can just put this inside a function:

function readclass() { read -p "What Is Your Profession? " a; case $a in "Theif") echo "Stealth";; "Cleric") echo "Heals?";; "Monk") echo "Focus?";; *) echo "invalid choice" a;; esac; }

Then use it:

export class=$(readclass)