In a bash script I have it is supposed to write certain lines to a file.
This is what it supposed to write:
echo "export ROS_PACKAGE_PATH=/home/forklift/forklift-workspace:$ROS_PACKAGE_PATH" >> ~/.bashrc
and this is what it writes in the file:
export ROS_PACKAGE_PATH=/home/forklift/forklift-workspace:
how do I fix this?
I'm using Ubuntu
Any help is appreciated!!
This is expected if the variable $ROS_PACKAGE_PATH is not defined (or equal to "") when echo is called. If I understand what you are trying to do, then you can just replace the double-quotes with single quote to prevent this behaviour. Thus
echo 'export ROS_PACKAGE_PATH=/home/forklift/forklift-workspace:$ROS_PACKAGE_PATH' >> ~/.bashrc
Should add the line
export ROS_PACKAGE_PATH=/home/forklift/forklift-workspace:$ROS_PACKAGE_PATH
to your .bashrc.
The $ROS_PACKAGE_PATH in the string is being interpolated as a variable, but it doesn't exist in the environment so it just comes out blank.
It needs to be escaped with a \, like this:
echo "export ROS_PACKAGE_PATH=/home/forklift/forklift-workspace:\$ROS_PACKAGE_PATH" >> ~/.bashrc
For more in-depth information on parameter expansion, this is pretty comprehensive: http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
Related
I'd like the include a dynamic variable in my PS1 prompt, let's say the 5th folder in the path. I'd also like to include some other PS1 codes (maybe color, username or current directory).
I have a script to get the 5th folder and echo it with one of the escape PS1 codes.
demo_prompt.sh
folder5=$(cut -d / -f 6 <<< $PWD)<br>
echo "$folder5 \W $ "
This .bashrc sets PS1 to the output of the script.
.bashrc
PS1='$(~/demo_prompt.sh)'
If I keep the PS1 definition in .bashrc in single quotes:
Pro: The 5th folder dynamically updates while I change directories as desired,
Con: \W appears in the prompt rather than resolving to the current folder name.
If I change the PS1 definition in .bashrc from single quotes to double quotes:
Pro: \W resolves properly to be the current directory
Con: The 5th folder is fixed to the value when I source .bashrc
How can I achieve both the \W resolving and the 5th folder dynamically updating?
I've more or less followed the idea here and am essentially asking the followup question that went unanswered. Bash: How to set changing variable in PS1, updating every prompt)
Quote: "I.e. it won't read in the escape codes nor color options. Any suggestions on this one?"
Thanks!
Try this :
demo_prompt.sh
folder5='$(cut -d / -f 6 <<< $PWD)\n'
echo "$folder5 \W $ "
and .bashrc
PS1="$(~/demo_prompt.sh)"
PROMPT_COMMAND='dir5=$(cut -d / -f 3 <<< "$PWD")'
PS1='$dir5 \W $ '
(Note the quotes)
PROMPT_COMMAND runs before the prompt is displayed and allows you to set global variables. You can also set it to a function call that would set global variables, call scripts, or call your script directly from PROMPT_COMMAND.
I am trying to echo a shebang to a file.
vincent#vincent-X751LJ:~$ echo "#!/usr/bin/sh" > file
echo "#/usr/bin/sh" > file
vincent#vincent-X751LJ:~$ cat file
#/usr/bin/sh
The "bang" vanishes (even in my history !). Everything looks like I never typed !. I have also tried without the double quotes. Same result.
It does not happen if I echo just #!
vincent#vincent-X751LJ:~$ echo "#!" > file
vincent#vincent-X751LJ:~$ cat file
#!
The reason is certainly so simple...
The only reference to a shebang echoing problem is on SO (How to echo a shebang in cmake COMMAND) but the problem is different on my side.
Thanks
I'm not sure why this happens exactly, but using single quotes (to disable shell escaping) should remedy this.
See Difference between single and double quotes in Bash
I want to add this new line to my .bashrc:
Path=$PATH:$JAVA_HOME/bin
I used:
echo "PATH=\"$PATH:$JAVA_HOME/bin\"" >> ".bashrc"
But it inserted:
PATH="/home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/bin:/usr/lib/java/jdk1.8.0_102/bin:/usr/lib/java/jdk1.8.0_102/bin:/usr/lib/java/jdk1.8.0_102/bin"
Anybody could help with this issue?
I know i could use a text editor like vim, but as is a part of a script i need to use the
echo "string" >> file
variant
Thanks and Greetings!
You need to escape $ for both $PATH and $JAVA_HOME
So it should be:
echo "PATH=\$PATH:\$JAVA_HOME/bin" >> ".bashrc"
Shouldn't you be escaping the dollar signs instead of adding quote marks and escaping those?
And your command should have an export in front of it so it would end up being echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> .bashrc
To keep it prettier than the other answers here, you can use single quotes because variables will not be expanded:
echo 'PATH=$PATH:$JAVA_HOME/bin' >> .bashrc
Additionally, you're gonna want to remove all of the sections in .bashrc from your previous attempts, as until you do that your PATH will get pretty cluttered with duplicate and incorrect entries as seen in your example.
I want to run a series of bash commands generated by a Python script. The commands are of the form export foo="bar" and alias foo=bar. They must modify the environment of the current process.
This works great:
$(./generate_commands.py)
until an export command contains a space e.g. export x="a b". This generates an error, and only "a is exported (quotes included).
Currently I'm working around this by outputting generate_commands to a temporary file and sourcing that, but is there a more elegant solution?
./generate_commands | bash
This will pipe the output of the script as input to bash
Edit:
To allow for variables to be visible in the current shell, you need to source the output:
source <(./generate_commands)
or
. <(./generate_commands)
I think the OP's problem is
cmd="export x=\"a b\""
${cmd}
does not work, but
export x="a b"
works. My way around this is
export x="a"
echo $x
x+=" b"
echo $x
I am using the sh 3.2 in Mac Os X. I have a file test.conf
config1="Configuration 1"
config2="a lot of text"
config3=...
So I only need to get the config1= and config2= parameter. How can I set a variable, that I can do this:
> echo $variable
Configuration 1
So simple, but I am not doing it work.
the sommand you are looking for is source
source test.conf
echo $config1 #echoes Configuration 1
if you need to have config1 in variable, add
varible=$config1
At a rough guess...
export `grep 'config1=' /your/config/file`
export `grep 'config2=' /your/config/file`
But remember if you put this in a shell script file, then you'll need to eval the file rather than execute it to set the variables in the current shell instance.
You could do this:
variable=`sed -n 's/^config1=//p'`
Or if you are attempting to evaluate certain parts of your file, try something like
eval `grep ^config1= test.conf`
to have config1=Configuration 1 evaluated by the current shell. (With the example you provided, this will cause a syntax error, because the value cannot contain unquoted whitespace.)
I generally recommend beginners to stay away from backticks, but this is a situation where they are a good answer.