Reputation: 13
I am running a command in a bash script that uses the content of a file as an argument. Here a simplified example:
USER=$(<$fileA)
PASSWORD=$(<$fileB)
wget -u $USER -p $PASSWORD
However, the content of the file can be set to any value by the user. I assume I need to escape this, but I don't know how to do that, especially in a way that $PASSWORD will NOT be altered. Otherwise the password will obviously not work.
Thanks
Upvotes: 1
Views: 284
Reputation: 2544
You probably simply need to add quotes around the variables.
wget -u "$USER" -p "$PASSWORD"
If you happen to capture a newline at the end of the variable, you may want to strip it with:
wget -u "${USER%$'\n'}" -p "${PASSWORD%$'\n'}"
The ${var}
syntax is equivalent to $var
but curly braces allows to manipulate variables with bash. By adding %$'\n'
before the ending curly brace, the %
means to remove the text $'\n'
, which means removing the trailing newline, if any.
In the worst case scenario, you may get newline and carriage returns (probably if the files were generated by a text editor in Windows), but you can also deal with it:
wget -u "${USER%%[$'\n'$'\r']*}" -p "${PASSWORD%%[$'\n'$'\r']*}"
This time around, the searched text is [$'\n'$'\r']*
which is means "the first newline (\n
) or carriage return (\r
) and everything after" and the %%
operator means "remove the longest sequence at the end".
Upvotes: 2