firepro20
firepro20

Reputation: 371

Assign ASCII values to variable in shell

I'm still new at shell scripting

I want to assign * to a variable an print it. Write now I'm just printing it with:

echo -e "\052"

Is there a way to assign that value to a variable?

Upvotes: 0

Views: 2241

Answers (2)

John Kugelman
John Kugelman

Reputation: 361565

Use $(cmd) or `cmd` to capture a command's output. The $(...) form is preferred because it's easier to nest.

var=$(echo -e "\052")

The shell will interpret escape sequences inside $'...'. That's single quotes with a dollar sign in front.

var=$'\052'

Or of course you could write the asterisk directly. Quote it to prevent wildcard expansion.

var='*'

When you print it, make sure to quote the variable. It's annoying to always have to type double quotes any time you use a variable, but it's usually the right thing to do.

echo "$var"    # yes
echo $var      # no

Upvotes: 4

Michael Albers
Michael Albers

Reputation: 3779

Using backticks, ``, allows you to capture the output of a command. Many shells have a more sophisticated syntax, $(). But backticks are the most portable.

var=`echo -e "\052"`

Upvotes: 0

Related Questions