This question already has answers here:
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 3 years ago.
I'm trying to use the hostname of a linux device into a string for that here's what I'm trying:
app.sh
flowname="flow_${hostname}.txt"
echo "$flowname"
running the script delivers the following output : flow_.txt
any idea what I'm missing here ?
thanks in advance !
There's no built-in variable named $hostname. Try the variable $HOST or the command substitution $(hostname) instead.
flowname="flow_$HOST.txt"
flowname="flow_$(hostname).txt"
The ${VARIABLE} syntax is used to substitute the value of the given variable.
You want to execute the hostname command.
By using the above variable substitution you try to output a variable called 'hostname' which does not exist.
You have to use the $(COMMAND) syntax. This will execute the given command and print its result.
Try "uname -n", the "uname" command gives interesting information on your host.
I tried your script under ubuntu on WSL (windows subsytem for linux) and found the comment of John Kugelman useful.
To make the script functioning in this environment, i first print out the available system environment variables.
printenv | less
then I chosen to replace ${hostname} with ${NAME}.
You need variables that are populated. Use the command env to list environment variables,
env
So, just populate hostname,
hostname=$(uname -n)
echo ${hostname}
flowname="flow_${hostname}.txt"
echo "$flowname"
Related
test.sh:
#! /bin/sh
me=I ./test2.sh
test2.sh:
#! /bin/sh
echo $me
run script 1 and printing this:
[zhibin#szrnd1 sh]$ ./test.sh
I
[zhibin#szrnd1 sh]$
As see, the variable "$me" be transferred to "test2.sh".
I didn't find this usage of variable definition on googling, can someone tell me where can find the tutorial including the usage above mentioned?
Thx a lot!
Since this has been mentioned on SO a lot, I'm assuming you're looking for some documentation on it. I'm not sure there is anything more detailed than the BASH documentation about this:
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.
As you've seen by experimenting, when you do "A=B command", it runs the command as if "export A=B" was run just prior to that command then A's value reverts to its previous after command completes. It's a very convenient way to pass some environment into a command while ensuring the rest of the script is not affected.
From the bash documentation on the Environment:
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.
So if you put variable assignments before a command, that command is run with those environment variables.
This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 2 years ago.
I have a seemingly simple bash script to setup my environment:
The first two lines are:
#!/bin/bash
export CVE_ENV_DIR=$PWD
easy, hey? Well, see what happens when I run it, I get the following output:
$ echo $PWD
/work/env
$ ./env.sh
$ echo $CVE_ENV_DIR
$
Why does CVE_ENV_DIR not get set to /work/env? What is happening here? When I type export CVE_ENV_DIR=$PWD manually on the shell, it works as expected...
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.
This question already has answers here:
What is the difference between $(command) and `command` in shell programming?
(6 answers)
Closed 4 years ago.
Hi I am trying to print the public ip of the machine in a file using shell script. I am using the command
ip=${curl ipinfo.io/ip}
in my script file and it gives an error saying bad substitution. whereas this command works when i run it in command line.
Is this the right way to get the ip through the script?
Thanks in advance!!
You are capturing the result of program so you should use $(). The following should work for you (with the -s parameter to curl stopping unnecessary output)
ip=$(curl -s ipinfo.io/ip)
This question already has answers here:
How to pass command line arguments to a shell alias? [duplicate]
(11 answers)
Closed 5 years ago.
I am new to linux and trying to create an alias that starts mongodb service.
The original command is sudo service mongod start. I want to generalise this usage for any service.
i.e. something like alias startservice="echo <password> | sudo -S $1 start".
So I would call it like startservice mongod should run the first command. I came to know that I can use functions for the same. However, I don't have a clue on how to do this either way.
Because I want my function which I create to be able to be accessible across terminals. I am not sure on how to create functions that act in this manner. Please help me on this.
I have gone through these two links:
parameter subsitution in bash aliases
Alias with Argument in Bash - Mac
Your help would be appreciated.
Example of function in bash:
startService(){
echo "your-password" | sudo -S service "$1" start
}
startService mongod # test
This question already has answers here:
How to get the default shell
(5 answers)
Closed 6 years ago.
I am composing a bash script to serve as a utility tools.
The challenges I am facing now is:
- user using my tool will be running in bash environment
- however, some of them might default using krcsh or tcsh. they might have aliases or configurations set in there.
So, I need to prompt/guide user to resolve this during installation. My first challenge: How am I suppose to know the user's default shell within my install.sh?
Knowing the "default" shell, I can prompt and guide the user to do necessary transfer to bash.
my testing code:
my result:
1/ is fault obviously. It return the current shell which is my install.sh (bash)
2/ I am doubtful. It seems to be the history of what I have run before. It does not show me my default configured shell. My case, my terminal default shell is bash, and I run tsch for testing purpose. So the script parsed wrong information and will though my default shell is tcsh. It will then assist me to port configurations from tcsh to bash during the installation process.
If you want to check the shell you are using, you can use the following methods:
echo $0 in terminal will show you the program running if you want to check the shell you are currently using.
echo $SHELL - with this command you can read the user's default shell in the terminal you are running.
If you want to prompt, easily you can put the echo $SHELL in the part of your script where you need to show the current shell you are using.
Don't forget to put #!/bin/bash if your script is designed to run in a bash shell!