Failed to source ~/.bashrc from bash script - linux

In my script, I try to source two files to fetch the variables.
But it failed to get the variables defined in ~/.bashrc.
OS: Ubuntu Desktop 20.04.2 LTS
$ cat debug.sh
#!/usr/bin/env bash
cat > ~/env.sh << EOF
VAR1="123"
EOF
echo "VAR2=456" >> ~/.bashrc
source ~/env.sh
source ~/.bashrc
set -u
echo ${VAR1}
echo ${VAR2}
$ ./debug.sh
123
./debug.sh: line 14: VAR2: unbound variable

In the ubuntu version, .bashrc will check whether it is running interactively, if it is not, it will do nothing. Non-interactive means you called it inside another shell, which you can just comment out the following lines in .bashrc.
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac

Related

Bash: Creating a shell variable in a bash script that I can access from command line

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.

Sourcing files in shell script vs sourcing on command line

I have the problem that my shell script is not acting exactly the same as my manual typing into a console. I am attempting to find and source some setup files in a shell script as follows:
#!/bin/bash
TURTLE_SHELL=bash
# source setup.sh from same directory as this file
_TURTLE_SETUP_DIR=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)
. "$_TURTLE_SETUP_DIR/turtle_setup.sh"
This bash file calls a .sh file:
#!/bin/env sh
_TURTLE_ROS_SETUP_DIR=$_TURTLE_SETUP_DIR/../devel
if [ -z "$TURTLE_SHELL" ]; then
TURTLE_SHELL=sh
fi
if [ -d "$PX4_FIRMWARE_DIR/integrationtests" ]; then
if [ -f "$PX4_FIRMWARE_DIR/integrationtests/setup_gazebo_ros.bash" ]; then
. "$PX4_FIRMWARE_DIR/integrationtests/setup_gazebo_ros.bash" "$PX4_FIRMWARE_DIR"
fi
fi
if [ "$TURTLE_SHELL" = "bash" ]; then
if [ -f "$_TURTLE_ROS_SETUP_DIR/setup.bash" ]; then
source $_TURTLE_ROS_SETUP_DIR/setup.bash
fi
else
if [ "$TURTLE_SHELL" = "sh" ]; then
if [ -f "$_TURTLE_ROS_SETUP_DIR/setup.sh" ]; then
source $_TURTLE_ROS_SETUP_DIR/setup.sh
fi
fi
fi
The line in question is:
. "$PX4_FIRMWARE_DIR/integrationtests/setup_gazebo_ros.bash" "$PX4_FIRMWARE_DIR"
I have made sure that this code is actually running and that my environment variables are correct. If I run this command on the command line everything works well. However, the same is not true when the file is sourced via shell script. Why is this? Is there something different about the environment of a shell script that is different from a command line. Also, how can I fix this problem?
Edit:
I am sourcing either the .bash or the .sh scale, depending upon which shell I am using.
Edit 2:
I am sourcing this script. Thus, everything is run in my default bash terminal, and it is all run within the same terminal and not a terminal spawned from a child process. Why is the script not sourcing setup_gazebo_ros.bash within the current shell?
It's the same reason why you source the env script and not run it. When you run the script it runs in a new shell and the variables are not transferred back to the parent shell.
To illustrate
$ cat << ! > foo.sh
> export foo='FOO'
> !
$ chmod +x foo.sh
$ ./foo.sh
$ echo $foo
$ source ./foo.sh
$ echo $foo
FOO

bash substring working in interpreter mode but not in shell script

When I try to do a substring in bash shell in interpreter mode, I get expected output
bash-4.2$ x="SomeString"
bash-4.2$ echo $x
SomeString
bash-4.2$ y=${x:0:4}
bash-4.2$ echo $y
Some
bash-4.2$
whereas while running the same commands in a shell script, I get an error.
bash-4.2$ cat shell.sh
x="SomeString"
echo $x
y=${x:0:4}
echo $y
bash-4.2$ sh shell.sh
SomeString
shell.sh[3]: y=${x:0:4}: 0403-011 The specified substitution is not valid for this command.
bash-4.2$
The irony is that when I invoke the shell by bash-4.2$ ./shell.sh, it's working.
What's happening here?
I'm on an AIX machine.
Substrings are a bash extension. When you run it as sh, it disables this extension. Use bash shell.sh and it will work.
You should also put #!/bin/bash at the beginning of the script, to ensure that it runs with bash when you invoke it as a command.

How can I tell a Linux Script (ash, not bash) is running "sourced"?

How can I tell in an ash script if it is running "sourced" or "normal"? By sourced I mean using the "." or "source" command to launch the script in the current shell.
Not sure if it's the best option (will not work if the script has the same name as the shell), but you can check the first parameter ($0). Example:
$ cat test.sh
#!/bin/ash
echo "Value: $0"
$ ./test.sh
Value: ./test.sh
$ source test.sh
Value: ash
If you want to check if the file was sourced, you can use something like this:
#!/bin/ash
case $0 in
ash) echo "Sourced" ;;
*) echo "Not sourced" ;;
esac

Bash file shows "ln: command not found"

I'm trying to create a bash script to setup my development environment. The script is running as root but I get the error line 11: ln: command not found
#!/bin/bash
#Require script to run as root - doesn't work - syntax error in conditional expression: unexpected token `;'
#if [[ $(/usr/bin/id -u) -ne 0]]; then
# echo "Script must be run as root";
# exit;
#fi
#PHPMyAdmin
PATH="/etc/apache2/sites-available/phpmyadmin.local";
if [ ! -a PATH ]; then
ln -s /home/user/Ubuntu\ One/htdocs/vhosts/phpmyadmin.local PATH;
a2ensite phpmyadmin.local;
fi
PATH=...
Congratulations, you've clobbered how the shell finds commands. Don't do that.
PATH tells the shell where to look for commands. In your case, it looks for ln somewhere in /etc and predictably doesn't find it there.
You should use a different name.

Resources