Reputation: 8576
This is so simple yet...
FOLDER='/home/user/.ssh'
SSH="$FOLDER/local-rsync-key.pub"
if [ -f "$SSH" ]; then
...
It looks that Bash considers the '-' as a minus signal and the IF statement always fails... How can I write this variable the right way?
UPDATE: This is another real example:
I am tring to rename files with "-" in front of the filename, for example: "-0001.jpg"
However, everyime I try to run:
for i in *; do mv "$i" "${i//-/}"; done
or:
for i in *; do mv "$i" "${i#*-}"; done
I got this error:
mv: invalid option -- '0'
Try `mv --help' for more information.
Thanks for any light!
Upvotes: 1
Views: 2102
Reputation: 8576
The answer is to use "--" (indicating no more options) after "mv" or "./" before the name of the file (indicating it is about a file). For example:
for i in *; do mv -- "$i" "${i#*-}"; done
or:
for i in *; do mv -- "$i" "./${i#*-}"; done
Upvotes: 3
Reputation: 881093
You should not have a $
in front of your SSH
assignment, that's only needed when you're using the variable. Without that, it works fine, as in the following transcript:
pax> touch abc-xyz
pax> ll a*
-rw-r--r-- 1 pax paxgrp 0 2011-06-24 05:15 abc-xyz
pax> FOLDER=.
pax> $SSH="$FOLDER/abc-xyz"
bash: =./abc-xyz: No such file or directory
pax> SSH="$FOLDER/abc-xyz"
pax> if [ -f "$SSH" ]
...> then
...> echo yes
...> fi
yes
pax> _
Upvotes: 3
Reputation: 5408
In bash syntax, when you set a variable just use the variable name:
VAR=value
When you reference the variable, use the $
prefix:
echo $VAR
Your code has a stray dollar sign prefix where you are trying to set the SSH variable. The dashes inside the variable should be no problem.
Upvotes: 1