2
OS: Ubuntu 14.04

I am new to shell programming (and I don't have time to become an expert, as this is something I do not envision doing in regular basis), but I need to do some stuff in scripts to prep for Rails app deployments. I have the following script:

#!/bin/bash
cd ~
sudo ls -l
read -p $'Enter version of Ruby you want to use, ex: 2.1.7\n' rubyv
if [ "$rubyv" = '' ]; then
  echo 'You did not enter a Ruby version to use, exiting...'
  exit
fi
echo '    You entered '$rubyv
read -rsp $'If this not what you wanted press any key to exit, otherwise, press c to continue\n' -n1 key
if [ "$key" != 'c' ]; then
  exit
fi

Then I would like to continue the script by using $rubyv to install Ruby using rvm, something like:

rvm install "$rubyv"

For a test, I tried entering a directory name that I created, and then added this to the end of the script:

cd "$rubyv"

But nothing is happening. What am I missing?

Solution:

It appears as the cd command is executing fine, but the reason I do not see it, is that when the script exists, I am returned to ~, the directory I started from. I verified that I was in the directory that I cd to, by doing an ls -l at the end of the script, and it did give me the proper listing

8
  • Did you put rvm install $rubyv in that script? Commented Oct 8, 2015 at 22:56
  • No, I used a test to see if it might work. Please see my edit Commented Oct 8, 2015 at 22:57
  • Add a pwd at the end of the script. It won't change the parent environment. Commented Oct 8, 2015 at 22:59
  • It's safe to use echo like this echo $rubyv. What do you mean by nothing happens. For me that script works and I can see output. How do you execute it? Is it executable? chmod +x script.sh Commented Oct 8, 2015 at 23:03
  • OK, I added rvm install "$rubyv" to the end of the script, and it worked. I don't understand why the cd command would not work. I need it for another part of the script. What do you mean by add pwd? Commented Oct 8, 2015 at 23:04

1 Answer 1

3

Each shell script runs in its own environment. It means that the variable $rubyv exists only inside the script.

Directory change can be done only inside the script. Once that script starts it changes directory in its environment to your HOME by cd ~. So, all commands inside the script are executed from HOME, for example ls -l (by the way it is strange to execute ls with sudo in your home directory).

After script termination the variable $rubyv is not valid and you are back in the directory where the script was executed.


It is also possible to excute script in the current shell without creation of subshell using dot space dot slash:

. ./scriptname

In that case all commands will be executed directly in the current shell, so it will affect the current environment and directory. However, it is better to run helper scripts in usual way (just by ./scriptname) to protect the current environment).

Sign up to request clarification or add additional context in comments.

1 Comment

The reason I am doing a sudo ls -l, is that there's a lot more to this script, and at one point, I am doing some stuff in the script that requires sudo level, and that would be 10-15 minutes down the road in the script, If I enter the sudo credentials early on, my script will not stop if I walk away from it. If you have another suggestion on how to handle this, without doing sudo su (Rails will not like that), it would be appreciated

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.