Another Bash string escape question (using echo)

BuvinJ

I'm trying to use this line in a bash script to update my cron jobs:

(sudo crontab -l ; echo "0 6 1-7 * * [ $(/usr/bin/date +\%u) == 7 ] && sh $script_path > $log_path") | sort - | uniq - | sudo crontab -

Prior to this, $script_path and $log_path have been defined. I know this works in general. I just have one hang up. $(/usr/bin/date +\%u) resolves to an empty string when the crontab file is written to. I need that literal text to remain in place.

(Not that is really matters, but the point of that is to verify the day of the week. This job is to be run on the first Saturday of the month).

How do I "escape" that whole substring?

xhienne

You may escape each problematic character with \ (see Hunter.S.Thompson's answer) or just use single quotes, the enclosed string being written as is:

(sudo crontab -l ; echo '0 6 1-7 * * [ $(/usr/bin/date +\%u) == 7 ] && sh '"$script_path > $log_path") | sort -u | sudo crontab -

I personally tend to avoid escaping characters, as I think it harms readability.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Echo in Bash: Escape Apostrophes?

From Dev

echo \\* - bash backslash escape behavior, is it evaluated backwards?

From Dev

bash echo into another file not working

From Dev

Echo multiline string into file bash

From Dev

Using single quotes with echo in bash

From Dev

Using alias to echo backticks in bash

From Dev

How to echo #!/ using shell or bash

From Dev

Echo variable using sudo bash -c 'echo $myVariable' - bash script

From Dev

Decoding string using HtmlDecode or Escape

From Dev

Echo a string with no spaces using PHP

From Dev

bash - quickly escape any characters string

From Java

How to echo a variable following a string in bash?

From Java

parse and echo string in a bash while loop

From Dev

Bash - Echo is splitting my string result

From Dev

Using Awk with escape characters inside a Bash script

From Dev

Using Awk with escape characters inside a Bash script

From Dev

Entering a folder with whitespaces and escape characters using bash

From Dev

How to escape asterisk and question mark using RLIKE in PDO?

From Dev

How to escape asterisk and question mark using RLIKE in PDO?

From Dev

Echo value from id using another query?

From Dev

Using a question mark as a String in android

From Dev

How to print #!/bin/bash using echo command

From Dev

using 'echo' & 'printf' in bash function calls

From Dev

Escape special characters in echo

From Dev

How to escape echo?

From Dev

Echo -e escape sequences?

From Dev

Correctly Escape echo -ne

From Dev

How to use subprocess.run to echo (bash) inside another file

From Dev

Bash: Echo a variable whose name is the value of another variable