This question already has answers here:
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 4 years ago.
I am trying to export few variables and invoke spark-submit using a shell script. However the export variables are not printing as expected. Please find below the code and help me in resolving the issue.
export BASE_LOCATION=/home/hduser/hdm
export EXT_LIB=$BASE_LOCATION/extlib
echo $EXT_LIB
export EXT_LIB_JARS=$EXT_LIB/common-csv-1.1.jar:$EXT_LIB/spark-csv_2.10-1.5.0.jar
echo $EXT_LIB_JARS
I was expecting an output as /home/hduser/hdm/extlib from echo $EXT_LIB.
But I am receiving output as /libe/hduser/hdm
Also echo $EXT_LIB_JARS is not giving desired results.
Please help me to resolve this issue.
Regards,
Adarsh K S
I'm getting the output you described when the BASE_LOCATION has $'\r' at the end (and $EXT_LIB apennds /lib to it, not /extlib).
#! /bin/bash
export BASE_LOCATION=/home/hduser/hdm$'\r'
export EXT_LIB=$BASE_LOCATION/lib
echo $EXT_LIB
Maybe you edited the script on MSWin and it inserted its line-endings into it? Or you extract the value from a file that comes from MSWin?
Just remove the \r's from the value.
Probably the file you are editing is for a different user or you are not logged in as the user for the conf file you are editing.
If it is then i would hazard a guess that there are many more lines and it is set somewhere after in the conf script as something else or even dynamically held until restart by setting from an install script.
Related
This question already has answers here:
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
Closed 1 year ago.
Issue
I want to run a bash file more easily, I've seen some applications where you only need to type word to execute the script.
Instead of typing ~/folder/file.sh in the terminal,
I only have to type a_word to run the file.
Is this possible with bash?
And also, this is on RPiOS's terminal, not sure if it differs.
Save your file to a location named in PATH. /usr/local/bin/a_word (no .sh) is a great example of such a location. Make sure it has executable permissions and starts with a shebang (like #!/usr/bin/env bash).
When you want to install something just for your own account, it's common practice to create a ~/bin directory and add it to your PATH (as by adding something like PATH=$PATH:$HOME/bin in ~/.bash_profile).
You have to define a so called alias.
Edit the file $HOME/.bashrc and add alias a_word = '$HOME/folder/file.sh', then logout and logon again.
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:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have created a virtual environment on my debian system and i made a script that activates it (should).
However when i execute the script nothing shows up, not even an error, my guess is that it is running in a different shell or something but I don't know how to solve it.
Here is the code of the script
#!/bin/bash
source ~/PythonEnv/environments/my_env/bin/activate
I have changed the permissions already with chmod u+x, so that is not a problem.
When i execute the script nothing shows up at all. Any thoughts???
Add set -x at the beginning of your bash script will do the trick.
-x Print commands and their arguments as they are executed.
You can see more bash options here
http://linuxcommand.org/lc3_man_pages/seth.html
Adding x-permissions is not necessary, since you are using source with an absolute path. Of course this sets the environment only which is executed by the shell script which you have posted here. If you want the changes in your interactive shell, it is pointless to do it inside a script. You have to source the activate script in your shell (respectively inside that process where you want the environment to be modified).
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 3 years ago.
I want to add a line to a file maintaining the exact pattern
Line i want to add:
export PATH=$PATH:$JAVA_HOME/bin
I dont want to add the values of the variables to the file
What I did:
echo "export PATH=$PATH:$JAVA_PATH/bin" | sudo tee -a /home/admin/Vishal/test.sh
My Output:
Contains numerous paths instead of export PATH=$PATH:$JAVA_HOME/bin
The immediate problem is that you need single quotes instead of double. But really, you should not be editing your script file. Instead, make it accept a parameter which tells it whether or not to update the PATH.
case $1 in --update-path) PATH=$PATH:$JAVA_HOME/bin;; esac
If you run /home/admin/Vishal/test.sh --update-path it will add the Java directory; without the option, it won't.
I was trying to permanently set environmental variables for a linux VM, and I edited the .bashrc file and added two variables there. To see these variables I used the
bash
echo $VARIABLE
to make sure that I set the variables correctly. Now I can't seem to exit the bash though. I don't know if editing these variables has messed up the system now or what. I used VI to edit the file originally, but now I can't undo the changes with VI or anything because I'm getting an error that -bash: vi: command not found.
So my question is, how do I exit bash mode?
Thanks
So the problem as was found was that I edited the PATH variable which then messed around with the entire shell.
In order to reset this I used the . /etc/skel/.bashrc command.
Thanks guys