TransWikia.com

Script to change current directory (cd, pwd)

Unix & Linux Asked by Sony Santos on November 23, 2021

I want to run a script to simply change the current working directory:

#!/bin/bash
cd web/www/project

But, after I run it, the current pwd remains unchanged! How can I do that?

11 Answers

Because I functionalized a lot my cd, i did this :
added this line in ~/.bashrc

alias cd='. my_cd'

and my_cd is a script in my $PATH that does the actual cd.
To prevent recusive calls an actual cd in the script is written cd this means "uses legacy cd not the alias".

By functionalized i mean

  1. jump to the home dir of an existing project just cd to its code.
  2. just cd in a project subdir brings me to the project home dir, not my home.
  3. cd to a project inexistent (project name have nomenclature): suggest to create the environement of the project.
  4. cd to a project that hapen to be archived : ask to revive it or just move to archive.

otherwise works like cd.

Answered by frederic abel on November 23, 2021

This combines the answer by Serge with an unrelated answer by David. It changes the directory, and then instead of forcing a bash shell, it launches the user's default shell. It however requires both getent and /etc/passwd to detect the default shell.

#!/usr/bin/env bash
cd desired/directory
USER_SHELL=$(getent passwd <USER> | cut -d : -f 7)
$USER_SHELL

Of course this still has the same deficiency of creating a nested shell.

Answered by Asclepius on November 23, 2021

For me the most convenient and flexible approach was a mixture of an alias and a script:

create script with arbitrary logic

Here I create a script that changes to a directory and activates the appropriate python environment. The scripts location is exmplary in /path/to/workon_myproj.sh.

#!/usr/bin/env bash

cd $HOME/workspace/myproj
source .venv/bin/activate

create alias that sources script

alias workon_myproj='source /path/to/workon_myproj.sh'

Add the alias definition into your appropriate shell start file e.g. .profile, .bashrc or .zshrc.

outcome

You can now simply execute workon_myproj in a shell which will source the content of your script in the desired directory.

extensibility

You could event improve your script to take an argument so that it works with multiple projects in a specific working directory, or combine it with a git pull to get the latest changes immediately and so on... everything boiler plate stuff you do when continuing to work on a specific project.

Answered by Jan on November 23, 2021

You can do that using a function or using && The examples bellow installs Zabbix and creates a file with a line inside it.

Ex:

#!/bin/bash

# Create Function:
installZabbix(){
    cd /usr/src/zabbix-4.2.4;
    ./configure --enable-agent;
    make install;
    cd /usr/src/;
    >file;
    echo "Hi, this is a file." >>file;
}

# Call the function:
installZabbix

or:

#!/bin/bash
cd /usr/src/zabbix-4.2.4 && ./configure --enable-agent && make install && cd /usr/src && >file && echo "Hi, this is a file." >>file

Answered by Raul Chiarella on November 23, 2021

Why not use "exec" it seams to do exactly what I wish.

#!/bin/bash

cd someplace
exec bash

~/someplace

Answered by paul on November 23, 2021

Depends on what you're going to do, another solution can be creating a function instead of a script.

Example:

Create a function in a file, let's say /home/aidin/my-cd-script:

function my-cd() {
  cd /to/my/path
}

Then include it in your bashrc or zshrc file:

# Somewhere in rc file
source /home/aidin/my-cd-script

Now you can use it like a command:

$ my-cd

Answered by Aidin on November 23, 2021

Use exec bash at the end

A bash script operates on its current environment or on that of its children, but never on its parent environment.

However, this question often gets asked because one wants to be left at the bash prompt in a certain directory after the execution of a bash script from another directory.

If this is the case, simply execute a child bash instance at the end of the script:

#!/usr/bin/env bash
cd desired/directory
exec bash

This creates a new subshell. Type Ctrl+D or exit to return to the first shell where the script was initially started.

UPDATE: Use $SHELL at the end

At least with newer versions of bash, the exec on the last line is no longer required. Furthermore, the script can be made to work with whatever preferred shell by using the $SHELL environment variable. This then gives:

#!/usr/bin/env bash
cd desired/directory
$SHELL

Answered by Serge Stroobandt on November 23, 2021

If you change between directories far away in the filesystem. I will recommend autojump.

Answered by stnly on November 23, 2021

While there are answers that do the exact action that you want, a more standard method for such purpose is to create symbolic link:

ln -s ~/web/www/project proj   #use full path to dir!

Then you could cd to the directory using the name proj:

cd proj

This method is more flexible because you could access files using the short name without cd:

ls proj/   #note the endslash!
vim proj/file.x

Answered by corvinus on November 23, 2021

For small tasks such as this, instead of creating script, create an alias like this,

$ alias cdproj='cd /dir/web/www/proj'

You should add this to your .bashrc file, if you want it set for every interactive shell.

Now you can run this as $ cdproj.

Answered by Sachin Divekar on November 23, 2021

It is an expected behavior. The script is run in a subshell, and cannot change the parent shell working directory. Its effects are lost when it finishes.

To change the current shell's directory permanently you should use the source command, also aliased simply as ., which runs a script in the current shell environment instead of a sub shell.

The following commands are identical:

. script

or

source script

Answered by enzotib on November 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP