Unable to set variables in bash script
Assignment in bash scripts cannot have spaces around the =
and you probably want your date commands enclosed in backticks $()
:
#!/bin/bash
folder="ABC"
useracct='test'
day=$(date "+%d")
month=$(date "+%B")
year=$(date "+%Y")
folderToBeMoved="/users/$useracct/Documents/Archive/Primetime.eyetv"
newfoldername="/Volumes/Media/Network/$folder/$month$day$year"
ECHO "Network is $network" $network
ECHO "day is $day"
ECHO "Month is $month"
ECHO "YEAR is $year"
ECHO "source is $folderToBeMoved"
ECHO "dest is $newfoldername"
mkdir $newfoldername
cp -R $folderToBeMoved $newfoldername
if [-f $newfoldername/Primetime.eyetv]; then rm $folderToBeMoved; fi
With the last three lines commented out, for me this outputs:
Network is
day is 16
Month is March
YEAR is 2010
source is /users/test/Documents/Archive/Primetime.eyetv
dest is /Volumes/Media/Network/ABC/March162010
How do I run a bash script and set variables?
To set environment variables that will be inherited by the script process, put the assignments at the beginning:
dbname="hello" dbuser="admin" bash wp.sh
How to get set variable in a shell script
By default variables declared without export
are not passed to subprocesses.
export s1="hello stackoverflow"
if it was set not by you , just export it:
export s1
./test.sh
---output
hello stackoverflow
s1='hello stackoverflow'
And of course use quotes:
#!/bin/sh
echo "$s1"
hello=$( set | grep s1 )
echo "$hello
bash script with variables not getting set
Child shells cannot affect the environment of their parent. If you want the script to affect the parent's environment, you need to:
source ./env.sh
So what's going on? When you run a bash script, either by bash env.sh
or env.sh
, you're spawning a program with its own environment, an environment that's divorced from its parent. But, when you run the commands contained in the script at the command line, or using source
, there is no spawned environment.
Edit to address @syme's comment. Bash scripts meant to be read using source
are often pure configuration, containing only assignments and calculations. It's possible to also make them a little more helpful and self-documenting with a clever she-bang hack like:
#!/bin/echo USAGE: source
# Default configuration file for the Frobnicator package.
FOO=bar
BAR=$(stat /baz)
[[ -f /baz ]] && BAZ=file || BAZ=
export FOO BAR BAZ
Making a bash script meant for configuration look like a configuration script, you help future maintainers. You also help yourself my modularizing your script code into distinct parts, each part with its one unique function.
As a side note, please don't export on the same line as you assign.
bash script - unable to set variable with double quotes in value
In bash (and other POSIX shells), the following 2 states are equivalent:
_account=foo
_account="foo"
What you want to do is to preserve the quotations, therefore you can do the following:
_account='"foo"'
How do I set a variable to the output of a command in Bash?
In addition to backticks `command`
, command substitution can be done with $(command)
or "$(command)"
, which I find easier to read, and allows for nesting.
OUTPUT=$(ls -1)
echo "${OUTPUT}"
MULTILINE=$(ls \
-1)
echo "${MULTILINE}"
Quoting ("
) does matter to preserve multi-line variable values; it is optional on the right-hand side of an assignment, as word splitting is not performed, so OUTPUT=$(ls -1)
would work fine.
Bash: Creating a shell variable in a bash script that I can access from command line
You have to export
the variable for it to exist in the newly-exec
ed shell:
#!/bin/bash
export mypath=$(pwd)
cd $1
echo $mypath
exec bash
Passing bash script variables to .yml file for use as child and subdirectories
Does envsubst
solve your problem?
For example, if I have a test-yaml.yml
that contains $foo
:
cat test-yaml.yml
output:
general:
$foo: argument_from_bash_script
rawdatadir: '/some/data/directory/$foo'
input: '/my/input/directory/$foo/input'
output: '/my/output/directory/$foo/output'
You can replace $foo
inside test-yaml.yml
with shell variable $foo
by envsubst
:
export foo=123
envsubst < test-yaml.yml
output:
general:
123: argument_from_bash_script
rawdatadir: '/some/data/directory/123'
input: '/my/input/directory/123/input'
output: '/my/output/directory/123/output'
Related Topics
Using Gzip to Compress Files to Transfer with Aws Command
How to Ask Bash for the Current Options
How to Offload the 1Hz Tick in Dyntick Mode
Multiple -A with Greater Than/Less Than Break Bash Script
Overhead of Supporting Floating Point Arithmetic Inside the Linux Kernel
How to Make a Bash String of Command with Redirect and Pipe
How to Scale Ejabberd Server MAChine on Centos to Handle 200 K Connections
Error Cl_Device_Not_Available When Calling Clcreatecontext (Intel Core2Duo, Intel Ocl Sdk 3.0 Beta)
How to Set the Socket Option So_Reuseport in Rust
Limit Output of All Linux Commands
X86_64 Assembly Execve *Char[] Syscall
Imagemagick: Generate Raw Image Data for PDF Flate Embedding
Sed Find and Replace Between Two Tags with Multi Line
How to Remove End Folder Name from a Path in Linux Script
Find and Replace a Word with Another in All File Names of a Directory