Tuesday, August 16, 2022
HomeNetworkingThe simplicity and complexity of utilizing quotes on Linux

The simplicity and complexity of utilizing quotes on Linux


There are only some particular characters concerned in working with character strings on the command line or in a script on Linux: the only quote, the double quote and the backslash. However the guidelines aren’t as apparent as one may assume. On this put up, we’ll take a look at the simple and the considerably difficult makes use of of those particular characters.

Echoing textual content

The echo command doesn’t require any number of quote characters a lot of the time. You’ll be able to echo a phrase like this with out bothering with quotes of any form.

$ echo Enter your identify:
Enter your identify:

If you wish to use quotes, there’s no downside with that. Both of those instructions will work:

$ echo “Enter your identify:”
$ echo ‘Enter your identify:’

In easy instances like these, it makes no distinction whether or not you employ single or double quotes. In truth, you’ll be able to even use each single and double quotes in a single echo command if you’re so inclined. Whereas the quotes don’t add something, they don’t get in the best way both.

$ echo “Make right now” a really ‘good day!’
Make right now an excellent day!

Enclosing one set of quotes inside the opposite, nonetheless, makes a noticeable distinction. The inside quotes might be retained. Listed below are two examples:

$ echo "right now is a 'good' day"
right now is a 'good' day
$ echo 'right now is a "good" day'
right now is a "good" day

If you want quotes

There are occasions when quotes are required. One instance is if you wish to use an apostrophe in a phrase. In any case, an apostrophe and a single quote are the identical in your keyboard, so you do not wish to confuse bash!

$ echo Please do not eat the daisies
>

Within the command proven above, bash assumes that you simply nonetheless intend to finish the quote that you simply began and prompts so that you can proceed. To keep away from having bash assume you’re beginning however not ending a quote, enclose the textual content inside double quotes and it’ll do what you meant:

$ echo “Please do not eat the daisies”
Please don’t eat the daisies

After all, the opposite factor that you are able to do is use a backslash character. This tells bash that you simply don’t need the apostrophe to be interpreted, however simply displayed.

$ echo Please don’t every the daisies
Please don’t eat the daisies

It’s also possible to use the backslash character to have a phrase or phrase displayed as a quote:

$ echo She likes to consult with herself as ”Mamacita”
She likes to consult with herself as “Mamacita”

Quotes and variables

Everytime you wish to assign multiple string to a variable, you could use quotes. In any other case, bash will assume that any strings after the primary are instructions that you’re attempting to run and won’t full the task of the worth.

$ when=subsequent week
bash: week: command not discovered...
$ echo $when

Enclosing the string that you’re assigning in quotes solves the issue. No errors concerned!

$ when='final week'
$ echo $when
final week

The quotes that you simply use additionally make a distinction when utilizing variables. Use double quotes if you wish to embrace a variable within the output.

$ right now=Saturday
echo "At present is $right now"
At present is Saturday
echo 'At present is $right now'
At present is $right now

The one quotes show the identify of the variable quite than displaying its worth.

Evaluating strings utilizing the if command

The selection of quotes additionally makes a distinction when you find yourself evaluating strings in an if command. If a variable has a multi-string worth, quotes are required, and the selection of quotes additionally makes a distinction. This works:

$ remaining=’the tip’
$ if [ “$final” == ‘the end’ ]; then
>    echo Goodbye for now
> else
>   echo huh?
> fi
Goodbye for now

Single quotes gained’t work for the comparability:

$ remaining="the tip"
$ if [ '$final' == 'the end' ]; then
>     echo Goodbye for now
> else
>     echo huh?
> fi
huh?

Why the distinction? As a result of, as prompt in an earlier instance, utilizing ‘$remaining’ in single quotes compares the string ‘the tip’ to the literal string ‘$remaining‘, to not the worth assigned to the variable. Echoing the variable in single quotes or passing its output to the octal dump (od -bc) command exhibits what the if command above is viewing:

$ echo '$remaining'
$remaining
$ echo '$remaining' | od -bc 0000000 044 146 151 156 141 154 012 $ f i n a l n 0000007

Wrap-up

Having each single and double quotes together with the backslash character obtainable means that you can work successfully with strings in bash. There are only a few guidelines that you could get it proper each time.

Be part of the Community World communities on Fb and LinkedIn to touch upon matters which might be high of thoughts.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments