# Assigning values with special characters to a variable

**URL:** <https://community.unix.com/t/assigning-values-with-special-characters-to-a-variable/397257>\
**Category:** Shell Programming and Scripting\
**Created:** [September 7, 2025, 4:49pm UTC](https://community.unix.com/t/assigning-values-with-special-characters-to-a-variable/397257 "2025-09-07T16:49:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie\_01](https://community.unix.com/letter_avatar/newbie_01/32/6_449c533f586939b6e5f246632a46421f.png) [@newbie\_01](https://community.unix.com/u/newbie_01)\
**Post date:** [September 7, 2025, 4:49pm UTC](https://community.unix.com/t/assigning-values-with-special-characters-to-a-variable/397257/1 "2025-09-07T16:49:08Z")

</div>

Hi,

I need to assign to a variable a value that has special characters in them, i.e. dollar, single quote, double quote, backticks etc that may have special meaning to the shell/OS.

At the moment, it appears like I have to either escape them, put the content of the variable value into a file and read from the file or use the read command to which I then enter the value of the variable.

Test script below:

```plaintext
$ cat test.bash
#!/bin/bash
# 

x="123456"
echo $x
x="1!23456"
echo $x
x="1!\\23456"
echo $x
x="1!\\2%^&*()'3456"
echo $x
x="1!\\2%^&*(\`)'3456" # Need to escape the \`
echo $x
x="1!\\2%"^&\*()34$5$6" # Need to escape " and $
echo $x
echo "## TEST get the variable from a file instead. Contents of x1 is as below:"
cat x1
echo "## HERE is the variable with a backtick read from x1"
x=$( head -1 x1 )
echo $x
echo "## HERE is the variable with a dollar and quote read from x1"
x=$( tail -1 x1 )
echo $x
echo "## TEST USING read command to prompt for x for variable with a backtick"
read -p "Enter value for x -> " x
echo $x
echo "## TEST USING read command to prompt for x for variable with a dollar and quote"
read -p "Enter value for x -> " x
echo $x

```

Runtime of the script:

```plaintext
$ ./test.bash
123456
1!23456
1!\\23456
1!\\2%^&*()'3456
1!\\2%^&*(\`)'3456
1!\\2%"^&\*()34$5$6
## TEST get the variable from a file instead. Contents of x1 is as below:
1!\\2%^&*(\`)'3456
1!\\2%"^&*()34$5$6
## HERE is the variable with a backtick read from x1
1!\\2%^&\*(\`)'3456
## HERE is the variable with a dollar and quote read from x1
1!\\2%"^&\*()34$5$6
## TEST USING read command to prompt for x for variable with a backtick
Enter value for x -> 1!\\2%^&\*(`)'3456 1!2%^&*(`)'3456
## TEST USING read command to prompt for x for variable with a dollar and quote
Enter value for x -> 1!\\2%"^&*()34$5$6
1!2%"^&*()34$5$6

```

While using the option to read the value from a file or using read works fine, is there possibly a 100% fool proof way of assigning the value inside the script besides having to escape the character that needs escaping?

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [September 7, 2025, 5:06pm UTC](https://community.unix.com/t/assigning-values-with-special-characters-to-a-variable/397257/2 "2025-09-07T17:06:50Z")

</div>

Use SINGLE quotes when assigning values.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [September 7, 2025, 7:32pm UTC](https://community.unix.com/t/assigning-values-with-special-characters-to-a-variable/397257/3 "2025-09-07T19:32:55Z")

</div>

Most important, use double-quotes around a `$var`

```plaintext
echo "$x"

```

To allow all characters, use redirection, from a file or from a here document:

```plaintext
IFS= read -r x << "_EOT_"
x_y"@%()#'`[]*{}\
_EOT_

```

---

<div class="post-metadata">

**Author:** ![newbie\_01](https://community.unix.com/letter_avatar/newbie_01/32/6_449c533f586939b6e5f246632a46421f.png) [@newbie\_01](https://community.unix.com/u/newbie_01)\
**Post date:** [September 19, 2025, 4:13am UTC](https://community.unix.com/t/assigning-values-with-special-characters-to-a-variable/397257/4 "2025-09-19T04:13:30Z")

</div>

Thanks [MadeInGermany](https://community.unix.com/u/madeingermany)

Your solution is the 'cleanest' one --- a bit lost trying to understand the use of IFS and the double quote for "_EOT_". I thought it might complain and look for a pairing "_EOT_". Are the double quotes required because of the use of IFS?

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [September 19, 2025, 4:42am UTC](https://community.unix.com/t/assigning-values-with-special-characters-to-a-variable/397257/5 "2025-09-19T04:42:50Z")

</div>

Without the quotes the shell would substitute `$` expressions in the here-document, like `$var` or `$(command)`  
Without the `IFS=` the `read` would discard leading space characters.
