I have variable difine as SCRPT_PATH="/home/dasitha" I need to add this path to .bashrc file using shell scirpt.
What I tired was something like this.
echo 'export PATH=$PATH:$SCRPT_PATH")' >> /root/.bashrc
After opening my .bashrc file it looks like this
export PATH=$PATH:$SCRPT_PATH")
What I actually need is export PATH=$PATH:/home/dasitha. How should I do this by changing the shell script?
You've got the wrong quotes (in addition to a spurious looking parenthesis and quote mark). You're looking for something more like
echo "export PATH=$PATH:$SCRPT_PATH" >> /root/.bashrc
Here's a quick example that demonstrates quoting:
krismatth#earth ~$ echo $HOME
/Users/krismatth
krismatth#earth ~$ echo '$HOME'
$HOME
krismatth#earth ~$ echo "$HOME"
/Users/krismatth
Related
I have a script, generate some output just as what the echo below does. How to export the two environment variables a and b?
I tried
echo -e "export a=3\nexport b=4"|bash
or
echo -e "export a=3\nexport b=4"|eval
or
echo -e "export a=3\nexport b=4"|exec
Neither works. Please help.
If you pipe the command to a program, the program runs in a child process, so none of its environment changes affect the original shell.
Use eval and give the string as an argument. Use ; to separate commands rather than newline.
eval 'export a=3; export b=4'
I have very little experience working with bash. With that being said I need to create a bash script that takes your current directory path and saves it to a shell variable. I then need to be able to type "echo $shellvariable" and have that output the directory that I saved to that variable in the bash script. This is what I have so far.
#!/bin/bash
mypath=$(pwd)
cd $1
echo $mypath
exec bash
now when I go to command line and type "echo $mypath" it outputs nothing.
You can just run source <file_with_your_vars>, this will load your variables in yours script or command line session.
> cat source_vars.sh
my_var="value_of_my_var"
> echo $my_var
> source source_vars.sh
> echo $my_var
value_of_my_var
You have to export the variable for it to exist in the newly-execed shell:
#!/bin/bash
export mypath=$(pwd)
cd $1
echo $mypath
exec bash
Hello
'env -i' gives control what vars a shell/programm get...
#!/bin/bash
mypath=$(pwd)
cd $1
echo $mypath
env -i mypath=${mypath} exec bash
...i.e. with minimal environment.
I am using csh terminal.
.cshrc
setenv $files /home/ec2-user/files
.login
if [ -f ~/.cshrc ]; then
. ~/.cshrc
fi
I am trying to echo $files values from plink.
It showing the error files: undefined variable
You don't use a dollar sign when setting a variable, you only use it when you refer to that variable.
setenv files /home/ec2-user/files
The test command should be a built-in in most csh/tcsh implementations, and has most of the same functionality you'll see listed under man test.
test -f ~/.cshrc && source ~/.cshrc
Note that normally, csh/tcsh will run your .cshrc or .tcshrc file automatically, before it runs .login.
In the shell script, I want to grep a value from the log file and set as the value of one environment. In the log file, there is one line like this:
SOME_PATH=/some/specific/path
In my shell script, I grep the SOME_PATH keyword, and I got the above line and split it with =, but I'm now able to set the environment variable:
line=`grep "SOME_PATH" /path/to/the/log.log`
path=${line##*=}
export SOME_PATH="$path" #I cannot change the environment variable with this line.
And if I just have the script like below, the environment variable changes.
export SOME_PATH=/some/specific/path
Exported variables available either in processes spawned from shell (script) which exported a var or in another script with sourced script with export statement.
1st way:
$ cat exp.sh
#!/bin/bash
export MYVAR="hi there"
bash
# or bash -c "echo here: $MYVAR"
2nd way:
$ cat exp.sh
#!/bin/bash
export MYVAR="hi there"
$ cat imp.sh
#!/bin/bash
. exp.sh
echo $MYVAR
Here's another explanation: advanced bash scripting guide.
I'd like to append the following lines to the end of my .zshrc file in an install script that is run:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
The .zshrc file has write only access by root, and I am another user (but with sudo access).
One way to do it is similar to the following:
sudo bash -c "echo 'export PATH=\"$HOME/.rbenv/bin:$PATH\"' >> ~/.zshrc"
The main problem is that the $HOME and $PATH fields, as well as the $() section are then inserted after being replaced with the interpreted values. I could put a single quote on the outside, but I need to use a double quote where the first single quote is currently, which then interprets the inside.
I'd appreciate any help about how to do this without interpreting the variables/commands before insertion. Is a heredoc an easier way to do this?
Escape variable expansion by placing \ before $:
sudo bash -c "echo 'export PATH=\"\$HOME/.rbenv/bin:\$PATH\"' >> ~/.zshrc"
Just use the correct quotes. And don't be afraid to switch between them.
sudo bash -c "echo 'export "'PATH="$HOME/.rbenv/bin:$PATH"'"'" >> ~/.zshrc"