user_rak
user_rak

Reputation: 97

navigate through folders using shell script

I am new to shell scripting. I have saved the script file as script_hdl in my home directory. From my home directory, I want to navigate using the script in the following order: cd ../../site/edu/ess/project/user/rark444

and then open a new tab from this new location in the terminal.

I used this as my script:

#!/bin/bash
alias script_hdl="cd ../../site/edu/ess/project/user/rark444"

I run the script like this

./script_hdl

But I don't see any response in the terminal. I feel I am missing something but I don't know what is it. Thanks in advance for your help.

Upvotes: 1

Views: 5323

Answers (4)

user3001290
user3001290

Reputation: 1

You can use cd++ for faster folder navigation. It can be found here

Upvotes: 0

Qeole
Qeole

Reputation: 9114

You have two ways to change directory here.

Script

The first one is to write a script, in such a way that you can run other command after cd. It works without the alias command: let's say you remove it.

cd command is proper to the running process. When you execute your script, the following happen:

  1. your shell spawns (forks as) a new shell process executing your code. The main process wait for its child to finish;
  2. this new child process actually does change its own working directory with your cd command, then quits (it's over)
  3. the original shell process stops waiting and prints the prompt again. But this process has not changed directory (only the child process did)

To perform what you want, (remove the alias command, then) call your script as follows:

source script_hdl

or with following shortcut:

. script_hdl

meaning that you want the instructions to run in the same shell process.

Alias

The second way to change directory is to use an alias. But you should not write your alias definition in a random script file, add it in your ~/.bashrc instead (this file is run each time you open a shell). So:

alias script_hdl="cd ../../site/edu/ess/project/user/rark444"

to reload ~/.bashrc:

. ~/.bashrc

And then don't try to execute from the file, just launch your alias as if it was a normal command:

script_hdl

Upvotes: 1

Gaurav Kumar
Gaurav Kumar

Reputation: 34

Looks like you are trying to set up an alias. You can do this by editing your .bash_profile file in your home directory (if it's not there you can create one and then run "source .bash_profile" after editing it) and make an entry like alias script_hdl='cd ../../site/edu/ess/project/user/rark444' and then run "script_hdl" from your terminal.

For more info on alias you can follow the link mentioned by Paul.

Upvotes: 1

Paul Cavanagh
Paul Cavanagh

Reputation: 5

Make sure the spelling is correct as unix is case sensitive and that you have permissions. First try it on the command line to ensure that it works, if there is an error it will appear on the command line as sometimes scripts hide the errors and messages. If it works then copy the text to the script file and don't use alias.

Here is the correct usage of alias

https://en.wikipedia.org/wiki/Alias_(command)

Upvotes: 0

Related Questions