• Uncategorized

About linux : BASH-replacing-quotes-and-checking-if-empty-closed

Question Detail

I can replace a single quote by two quotes.
But I also need to check if a value is empty.

Following code returns:
it’s: command not found

original="it's smth"
echo "$modified"

What is wrong?

Question Answer

In the third line, remove the leading $.

The way bash is reading your commands:

original="it's smth" bash received a variable called original and all is well

modified=${original:-NULL} bash receives a second variable called modified that defaults to the first.

$modified=${modified//\'/\'\'} bash is told to process/run something because the line begins with $modified because of the $. Taking the previous string as now commands, it gets stuck right away because the first piece of that command it's is indeed not found among its known commands.

echo "$modified" is returned without an error only because here – after echo – the $ is a reference to your second line, telling bash to reproduce that variable called ‘modified’ that was equal to ‘original’. The output is it's smth

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.