Set environment variable with space in Linux
You should do
export VIDEO="/home/mehrabib/my video"
and to sum Dan's comments up also do
cd "$VIDEO"
which will expand to
cd "/home/mehrabib/my video"
again.
Personally, I've come to prefer the ${VIDEO}
syntax.
How to set environment variables with spaces?
The error output by command setx is caused by wrong usage of the quotes on assigning the string to variable value
.
The command is set and the parameter is variable=value
. As for most commands and applications it is possible and often required to surround a parameter with double quotes if containing 1 or more spaces or any other character from this list: &()[]{}^=;!'+,`~
. Those characters are displayed on last help page output by running in a command prompt window cmd /?
or help cmd
.
But wrong is here:
set value="Hello world"
With first double quote after the equal sign the entire parameter variable=value
of command set is not enclosed in double quotes.
This results in interpreting the double quotes as part of the string to assign to variable with name value
. Everything from the equal sign to end of line including the double quotes and possibly existing trailing spaces and horizontal tabs is assigned here to variable value
instead of just the string Hello world
as expected.
On expanding the line
setx -M srijani "%srijani%;%value%"
the result is therefore:
setx -M srijani "Value of variable srijani;"Hello world""
And command setx interprets the wrong quoted parameter as syntax error.
Correct would be using:
set "value=Hello world"
Now the entire parameter of command set is enclosed in double quotes. Therefore ignored on parsing the line are:
- all spaces/tabs between command set and the first double quote,
- the first double quote,
- the last double quote,
- and all perhaps existing spaces/tabs after last double quote.
So just Hello world
is assigned to a variable with name value
.
For more details about correct assignment of a string to an environment variable read answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It contains also a simple demo batch code.
Some more information:
How an argument string containing 1 or more quotes somewhere in the middle is interpreted depends on command respectively application. The behavior on interpreting an argument with 1 or more "
within an argument string can vary depending on used compiler as explained in an answer on batch file: list rar file in specific folder and write result into text file and of course the source code of the command / application.
For most commands and applications the correct syntax is:
command "parameter in quotes"
"Path to application\app.exe" "parameter in quotes"
But there are applications which require quotes in the middle of an argument string. An example of such an application is Windows Explorer.
The following syntax is required to open an Explorer window from within a batch file with current directory displayed in window.
explorer.exe /e,"%CD%"
Not working are:
explorer.exe "/e,%CD%"
explorer.exe /e "%CD%"
So explorer.exe
requires that the directory to open is specified after /e,
with quotes in the middle of parameter string or it interprets "/e,%CD%"
respectively "/e %CD%"
as name of the directory with path to display in Explorer window.
See also SS64 - Windows Explorer command-line options.
How to export environment variable with space in path on terminal
Single quote and double quote , everything works:
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$ echo $BASH_VERSION
4.4.12(1)-release
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$ export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$ echo $JAVA_HOME
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$ export JAVA_HOME='/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java'
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$ echo $JAVA_HOME
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
[iahmad@web-prod-ijaz001 ~]$
[iahmad@web-prod-ijaz001 ~]$
Can a shell variable have spaces in name?
No, this is not possible, as a variable in bash cannot have a name that includes spaces:
name
A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore.
Name
s are used as shell variable and function names. Also referred to as anidentifier
.
env
, as you noticed, plays by different rules:
Environment variable names can be empty, and can contain any
characters other than ‘=’ and ASCII NUL. However, it is wise to limit
yourself to names that consist solely of underscores, digits, and ASCII
letters, and that begin with a non-digit, as applications like the shell
do not work well with other names.
(source: info '(coreutils) env invocation'
; emphasis mine)
Case in point, here's another post with an answer about how much of a pain it is to grab the output of an environment variable with spaces
Setting variables with spaces within .env
If your command is just a shell command, you could run your command in a subshell like this:
( . .env ; echo "$TEST" )
The source
or .
builtin has no problem with assignments containing spaces. It will set the variables in the .env
file in the current shell's environment.
In the more likely case of calling an external program, you'll also have to add 'export' to each assignment in your env
file like this:
export TEST="hello world"
This is necessary because source
does not export assigned variables as env
does, i.e. they are set inside the subshell only but not in the environment of another process started inside that subshell.
Access env variable with space in .htaccess
I have resolved this issue with following changes:
In my /etc/environment I used quotes for variable values having blank space.
export SITE_NAME=Bliss
export SITE_IDENTITY="Bliss Five"
In .htaccess also I used quotes for the variables having blank space
SetEnv SITE_NAME ${SITE_NAME} #This works fine
SetEnv SITE_IDENTITY "${SITE_IDENTITY}" #This works fine now
This resolved my issue. Cheers!!!
Save path with spaces in Makefile variable
You could replace:
SHELL:=/usr/bin/env bash
with one of these (or with whatever path directly leads to Bash's executable):
SHELL:=/bin/bash
SHELL:=/usr/bin/bash
SHELL:=bash
which would eliminate the space from the equation.
That being said, you should also be able to run the commands directly, i.e.:
./utils/tests.sh
instead of:
${SHELL} ./utils/tests.sh
which would eliminate the need to set and use variable SHELL
altogether.
Set an environment variable containing a sequence of multiple spaces
$ text="multiple spaces string example"
$ echo $text
multiple spaces string example
$ echo "$text"
multiple spaces string example
Related Topics
How to Connect to the Fitbit Zip Over Bluetooth 4.0 Le on Linux with Bluez
Url Encoding a String in Bash Script
How to Download the Torvalds Linux Kernel Master, (Re)Compile It, and Boot It with Qemu
How to Display Only Files from Aws S3 Ls Command
How Much Does Using Htaccess Files Slow Down Website Performance (Especially with Solid State Disks)
What Is the Purpose of Features.H Header
Why Doesn't "History | Vim" Work
Change .Eclipse Folder in Linux
Renaming a Set of Files to 001, 002,
Receiving Udp Broadcast Packets on Linux
Find Out If File Has Been Modified Within the Last 2 Minutes
Setup Cron Tab to Specific Time of During Weekdays
Force Gnu Linker to Generate 32 Bit Elf Executables
Hadoop: Require Root's Password After Enter "Start-All.Sh"