Abdelrahman Ismail
Abdelrahman Ismail

Reputation: 144

How to open specific folder using my custom bash script

I need how to write a Bash script (not an alias) to navigate to my projects folder.

For instance, when I write (without any arguments)

$Projects

it should run the following command

$cd ~/Abdelrahamn/Projects

but if I write an argument like this

$Projects Proj1

it should run the following command

$cd ~/Abdelrahamn/Projects/Proj1

Upvotes: 0

Views: 112

Answers (3)

a172
a172

Reputation: 185

Depending on your need you a hash may be a good option. A whole script for this seems like overkill. In your ~/.zshrc.local (or earlier in whatever script is calling this), add:

hash -d project=~/Abdelrahamn/Projects

then you could use cd as you normally would call it, but with the nameddir/hash at the beginning. For example:

cd ~project
cd ~project/foo

would translate to:

cd ~/Abdelrahamn/Projects
cd ~/Abdelrahamn/Projects/foo

Upvotes: 0

Joao Morais
Joao Morais

Reputation: 1925

Your script should have something like this:

cd ~/Abdelrahamn/Projects/$1

If a function inside ~/.profile or ~/.bashrc is accepted this would be as simple as:

Project() { cd ~/Abdelrahamn/Projects/$1; }

Upvotes: 2

stormbard
stormbard

Reputation: 56

You would need write a function to accomplish this and put it in your .bash_profile or .bashrc. Shell scripts execute in a subshell and once the script exits the subshell exits leaving you in the interactive shell.

Project () {
    project_path="/home/user/path/to/projects/"

    if [ $# -eq 1]; then
        cd "$project_path$1"
    else
        cd "$project_path"
    fi
}

Upvotes: 1

Related Questions