2

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.

2
  • Could be an XY problem. Can you explain how you would use this variable? Commented Jun 21, 2020 at 0:34
  • Running this script normally starts a new shell in a subprocess (or, more precisely, when you run this script it runs as a subprocess, and the exec turns that subprocess into a new interactive shell). That new shell will be missing any (non-exported) variables from the original shell (along with any special modes set in that shell). If you run the script five times, you'll wind up 5 levels deep in subprocesses. Then, as you exit those shells it'll drop back to the parent shells one level by one. This is probably not what you want. Commented Jun 21, 2020 at 2:02

4 Answers 4

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

This is generally the best way to have a "script" set variables in an existing shell. Note that the "standard" command to do this is . (yes, a period is a valid command), but some shells like bash allow source as a synonym (with some subtle differences).
1

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.

Comments

0

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

1 Comment

Note that the export is not strictly necessary. One can also do mypath=$(pwd) exec bash.
0

I don't know if you need to start a new shell every time you run a script, don't recommend this.

I think the better solution is using source instead of bash.

Example:

$ cat script.sh

#!/bin/bash
mypath=$(pwd)
cd $1
echo $mypath

To run it: source script.sh

Now, this will update your terminal and add this variable without starting a new process.

enter image description here

1 Comment

Please do not use screenshots when you can avoid it. It would be better to replace it with a code block. Other thing: Your answer does not add anything to what previous answers already said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.