I'm writing a simple script that prompts the user for information and saves it as a variable to be passed as arguments in another command. One of the variables stores a path which may include a space. Using the read
command's -r
tag, the path is retrieved as 'raw' input, ignoring the backslash escape character.
For example:
read -p "Enter checksum algorithm number (1, 224, 256, 384, 512, 512224, 412256): " -a shaa
read -p "Enter a file path: " -re shapath
The problem is I cannot figure out how to run the command with those arguments without it reading the escape character. Solved per @heemayl
I then need to store the output or return value of the command as another variable.
For example:
foo=$(shasum -a "$shaa" "$shapath")
However, when I echo $foo
I do not get the expected output of shasum -a "$shaa" "$shapath"
, but instead get shasum: /.../checksum.dmg
I was able to get it to work by using eval
and piping it into var
as shown below:
eval shasum -a $sha $shapath > val
I've read I should avoid eval
at all costs and it does not allow me to continue forward with the script.