I am making a bash script but I have some variables that need spaces as the name, how can I make it so that a space does not take it as another parameter example
#!/bin/bash
mkdir $1
cp -a /home/x/Descargas/archivo.zip /var/www/html/
unzip /var/www/html/$1 /var/www/html/$1/
php /var/www/html/$1/install/index_cli.php --domain=$1 --db_server=$2 --db_name=$1 --prefix=$3 --db_user=$4 --db_password=$5 --email=$6 --name=$7
the moment I run it it is something like this sudo ./script.sh varlor1 value2 value4 value5 value6 value7
but if it were something like sudo ./script.sh -value 2 -value3 -value4 etc that I can enter spaces and don't take the next parameter until it is separated with a - thanks
To add arguments that include spaces, use double quotes:
"va lor"
You can refer to this question for more information on arguments with spaces and special characters: https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other- special-characters
The IFS variable ( internal field separator ) determines the behavior of what you enter as arguments, that variable changes depending on the shell you use. If you use
bash
, the IFS is<espacio><tab><salto de línea>
, instead, if you usezsh
is<espacio><tab><salto de línea><carácter nulo>
.As a proof of this you can run
$ echo -n "$IFS" | od -cb
in your terminal and see what I comment.The IFS variable is an environment variable (which you can edit as you like, but be careful) that shells use their characters as separators after expansions (brace, pathname, command substitution, process substitution, variable expansion, etc).
Quotes prevent 2 things: 1. Certain expansions, for example, using "*" inside a variable prevents it from being expanded, 2. It prevents it from being split according to the characters described in the IFS.
So your script should look something like this.
Back to the subject of the IFS. If you made a script like this.
And you run it like this:
The result would respect the characters you entered, resulting in something like this:
Instead, if you change your script to this (by removing the quotes from the parameter substitution)
And you run it in the same way as the previous one, the output would be like this:
That is, it uses the characters of the IFS variable to separate blocks of text.
You can consult the documentation on this effect
man 1 bash
in the "QUOTING" section.Going back to your way of presenting your request, I think you had the concern that the parameters are taken into account within your program depending on a flag. And I very much agree with you on that, only I like it to be much more verbose to avoid confusion between the sysadmin and developers who use the program. So I suggest you apply the strategy of putting named parameters, I use this in the following way (I like it better than using getopts).
And, as long as you keep your variables in quotes, it will respect all IFS characters.
The way to run this program (thinking of an exaggerated example) would be something like this: