share
Unix & LinuxCommand Substitution Within a Command Substitution
[+2] [1] user847
[2020-04-18 23:41:09]
[ bash shell-script command-substitution ]
[ https://unix.stackexchange.com/questions/581021/command-substitution-within-a-command-substitution ]

I'm new to shell scripting and wanted to insure I haven't made any errors in creating this script for making a borg backup to a flashdrive that I plug into my computer.

#!/bin/bash

echo "$(borg create /media/$USER/Flashdrive/backup::$(date +%FT%H%M) /home/$USER/Documents)"
The ability to be easily nested is one of the reasons often given for preferring this form of command substitution over the older "backtick" form. See for example What's the difference between $(stuff) and ` stuff `? - steeldriver
The echo and the first command substitution are not needed (but the outer quotes around the command substitution would be correct). And borg allows a few placeholders in the archive name, so you could write the date as {now:%Y-%m-%dT%H%M} (maybe %F instead of %Y-%m-%d works too, I just haven't seen an example in the manual). - Freddy
Why did you use curly braces: {now:%FT%M%H}. instead of $(now:%FT%M%H)? - user847
Freddy, would you mind posting as an answer? If I understand you, you recommended this formatting: borg create /media/$USER/Flashdrive/backup::"$(date +%FT%H%M)" /home/$USER/Documents - user847
Why are you using echo? Rather than echo $(borg …), why not just run the borg … command? - G-Man Says 'Reinstate Monica'
[0] [2020-04-19 00:54:30] kosolapyj

This looks ok. You can easily try it by replacing borg with echo and see if this is what you are looking for.

Double quotes are not required.


Replace borg with echo? Like this: echo $(echo create /media...) - user847
Yes, and you'll see the output right the way - without being afraid by whatever borg might do. - kosolapyj
1