Originally Posted by mrbutabara
hi everyone. Im sort of a newbie to linux and i was wondering if its possible to use the import command in scripting to use commands such as cd in a shell script.
You seem to already understand why it doesn't work the way you want. So I will spare you that explanation.
By the time you are inside the script a new shell has already been spawned, so it is already too late to prevent that. In other words: nothing that you put inside the script can prevent it from launching a new instance of the shell, hence what you ask for is impossible by design.
However, you can do so from outside by sourcing the script. If you use this line into a script called test.sh:
Then source it:
It will work as expected. There's a short hand for "source", a simple dot:
Another possibility is to put an alias or a function in your ~/.bashrc or ~/.bash_profile files (read the INVOCATION section of the bash man page to get a complete picture of the bash initialization files and about aliases and functions). The shell rc files are sourced every time you open a new shell, hence, all the aliases and functions you drop into them will be available in every new shell you open.