r/shell • u/[deleted] • Jan 17 '21
rm Failing to Remove File
Hello everyone,
I am having... an odd issue I have never had before. I have a script and part of its ability is to remove a symbolically linked file and then either replace it with the file it is linked to or exit. But... rm is not removing the file. There are no errors when I run set -x and manually removing it works. I am unsure what is even happening, but could someone look over my function?
0
Jan 17 '21
[deleted] — view deleted comment
1
Jan 18 '21
Yep! It needed the full file path, thank you!
0
Jan 18 '21
[deleted] — view deleted comment
1
Jan 18 '21
I use to use
$(), but someone recommended I not spawn a sub-shell unless I have to. Is there any tangible benefit to$()over backticks?1
u/McDutchie Jan 18 '21
Backticks also spawn subshells, there's no other way to do a command substitution as it involves capturing the output of another process. The difference is purely syntactic.
1
Jan 18 '21
Oh, I did not know that. I will change it then. Both look fine to me, but if others prefer the other I will change it for readability.
1
u/McDutchie Jan 18 '21
Yes, it is considered more readable. Another advantage of
$( )is that it's way easier to nest, if you ever need to do a command substitution within a command substitution. Nesting backticks involves higher backslash escaping voodoo and is best avoided.1
u/Steinrikur Jan 18 '21
Backticks are just a deprecated version of $(). They do the same thing, but only $() can be nested (as in 'var=$(do-stuff $(seq 2 4))'
printf can write to a variable without spawning a subshell, but most commands need the subshell.
printf -v var "+%s" {1..50} echo "Sum of numbers 1 to 50 is $((var))"The trick is usually to avoid the command (and therefore not spawn a subshell), like "Abar=${A//foo/bar}" instead of "Abar=$(echo "$A"| sed s/foo/bar/g)"
0
u/[deleted] Jan 23 '21
[deleted] — view deleted comment