2

I am creating a script that needs to generate commit messages based on timestamps for automatic backups in a private repository.

echo "#!/bin/bash   
     cd ${DIRECTORY}
     git add .
     git commit -m 'Backup $(date +%s)'
     git push origin master
" >> backup.sh

The timestamp $(date +%s) variable must be printed as is in the file instead of printing the timestamp when this file is generated.

How do I pass the actual variable to the text file in bash?

I have tried back ticks and it prints nothing. Maybe I am trying it wrong?

mahradbt
  • 364
  • 1
  • 2
  • 12
Clarice Bouwer
  • 3,631
  • 3
  • 32
  • 55

3 Answers3

3

how about using single quotes instead of double quotes because

Single (') quotes won't interpolate anything, but double (") quotes will refer some more here

echo '#!/bin/bash   
     cd ${DIRECTORY}
     git add .
     git commit -m "Backup $(date +%s)"
     git push origin master
' >> backup.sh

on a completely different note '>>' do you really want this to be an append operator ?

asio_guy
  • 3,667
  • 2
  • 19
  • 35
  • That is very handy information, thanks! That worked. – Clarice Bouwer Nov 28 '18 at 05:10
  • Not me for the downvote :( So >> is append right? I'll do some more research. – Clarice Bouwer Nov 28 '18 at 05:11
  • The string you are enclosing in single quotes contains single quotes, which need to be escaped. You need to either escape the `$` characters in a double-quoted string, or escape the `'` characters in a single-quoted string. Also, this answer prevents `${DIRECTORY}` from being expanded, which was not indicated as a problem in the original question. – paddy Nov 28 '18 at 05:14
  • @paddy You can't escape a single-quote inside a single-quoted string, since escapes aren't recognized there. But it looks to me like the argument to `git commit -m` should be double-quoted instead of single-quoted, so it's a moot point. – Gordon Davisson Nov 28 '18 at 05:22
1

You also have the option of using a heredoc to append to backup.sh. A heredoc provides the additional control of allowing tab indention preservation using <<- and providing control over variable expansion by single quoting the heredoc sigil (e.g. 'EOF')

In your case if you wanted to expand ${DIRECTORY} but preserve $(date +%s) you could simply use:

cat << EOF >> backup.sh
#!/bin/bash   
cd ${DIRECTORY}
git add .
git commit -m 'Backup \$(date +%s)'
git push origin master
EOF

Which would append the contents to backup.sh with the proper directory and $(date +%s) instead of the timestamp.

If you want ${DIRECTORY} and $(date +%s) to both be input into backup.sh -- as written -- without expansion, simply single quote the first 'EOF', e.g.

cat << 'EOF' >> backup.sh
#!/bin/bash   
cd ${DIRECTORY}
git add .
git commit -m 'Backup $(date +%s)'
git push origin master
EOF

If it helps you think about what is actually happening you can also write:

cat >> backup.sh << EOF
...
EOF

(if you like to think about "everything between EOF ... EOF going into backup.sh)

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • The single quotes around the commit message prevent the `date` command from executing at all. – tripleee Nov 28 '18 at 09:02
  • @tripleee - not so, they are treated as single-quotes within double-quotes if the sigil is unquoted. – David C. Rankin Nov 28 '18 at 09:05
  • But in your second example, it is quoted. Oh well, I guess we are both second-guessing what the OP actually wants. – tripleee Nov 28 '18 at 09:08
  • Yes, and neither will expand -- which is I think what the question was asking for. In the first with the unquoted sigil, the `${DIRECTORY}` was left to expand, but the command substitution around `date` was escape to prevent expansion as the question described. Being unclear on whether the question wanted only one, or both, I gave both examples. – David C. Rankin Nov 28 '18 at 09:11
0

Since the $ indicates an expansion (in this case $(..) executes the date program), you simply need to escape the character so it's translated as a literal $.

To do this, prefix it with a backslash: \$

paddy
  • 60,864
  • 6
  • 61
  • 103