0

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?

0

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Best advice is from the bottom up.
0

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"`

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.