-2

here is a small test I ran, to try and move me back to root:

#!/bin/bash
pwd
cd /
pwd

It returns the current working directory, and then the correct new one, but when the script finished executing, I am still in the original directory and haven't moved. Is there any way to achieve this?

Thanks.

1 Answers1

4

There is no way to achieve this within a script - scripts start subshell, which is a standalone environment. There's all sorts of reasons for this, but pretty fundamentally - a script cannot tamper with your environment (including your cwd).

The closest you get is creating an alias within the current shell.

alias chr="cd /"

Either that, or 'source' the script, it "runs" in the current shell. E.g. like your .bashrc.

Sobrique
  • 4,424