6
6
6elkka2017-02-17 18:18:38
linux
6elkka, 2017-02-17 18:18:38

How to write a bash program (executable file) that changes directory?

I want to write a bash script that, among other things, should change the current directory. Then I want to make this script executable, write a shebang first and put it in a folder in my $PATH, to call from anywhere.
However, when trying to do this, I encountered the fact that the directory does not change, because this script is called in a separate interpreter (I wrote in shebang #!/bin/bash).
How to write a program that changes the current directory (if possible O_o)?
I don't like the option to make the script a function and add it to .bashrc, because .bashrc is executed before the interpreter starts and thus increases the timeout every time the terminal is started. Therefore, the task is set in this way. I am interested in the theoretical side of the issue.
UPD
Clarification of the question:
- you need to create an EXECUTABLE FILE
- this very EXECUTABLE FILE is supposed to be put in one of the PATH folders and called from anywhere in the terminal
- EXECUTABLE FILE, when called, will automatically shut down and change the current working directory
- EXECUTABLE FILE must be written in bash (and be with the necessary shebang in this case, as I believe)
UPD2
The last condition (the EXECUTABLE FILE must be written in bash) is not critical.
And there is no need to explain to me how to solve the practical problem in the course of which this theoretical one arose.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
Mystray, 2017-02-18
@6elkka

What you want is not well solved the way you want.
But if you want the strange so much, then the example is googled in the very first link.
The binding for how to pull out the PID of the parent, write yourself.
unix.stackexchange.com/questions/281994/changing-t...

N
ns5d, 2017-02-17
@ns5d

cd /to/you/path/

C
CityCat4, 2017-02-17
@CityCat4


The option to make the script a function and add it to .bashrc does not suit, because, firstly, .bashrc is executed before starting the interpreter and thus, if it is lengthened, the waiting time increases every time the terminal is started
Laughing, right? Well, the launch of the shell will be lengthened by a couple of microseconds - really notice?

Z
Zr, 2017-02-17
@Zr

> it is required to create an EXECUTABLE FILE
> this same EXECUTABLE FILE is supposed to be put in one of the PATH folders and called from anywhere in the terminal
> EXECUTABLE FILE when called, shut down and change the current working directory
This is impossible. Well, or at least it should not be possible without the use of very spreading crutches such as program recording for teletype input.
The working directory is a property of the process's environment, and just like the entire environment, it is inherited by child processes, not shared with them. And an executable file, by definition, runs in a child process.

A
abcd0x00, 2017-02-18
@abcd0x00

How to write a program that changes the current directory (if possible O_o)?

#!/bin/bash

cd /

echo "I'm in `pwd`"

bash

exit 0

I
ipc_ngs, 2017-02-18
@ipc_ngs

In theory, you can use the source (or .) command to call the script:

$ cat ~/bin/tt.sh
cd /tmp

$ pwd
/
$ . tt.sh
$ pwd
/tmp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question