1

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.

user_rak
  • 85
  • 4
  • 17
  • 1
    That's not a script that's an alias. Running `./script_hdl` should give you an error unless a file with that name exists in the current directory. If you run `script_hdl` that will run that `cd` command instead (which will only work in whatever directory makes that path correct). – Etan Reisner Jul 12 '15 at 17:15
  • yes, I have script_hdl in the current directory which is my home directory. i read from here [http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script] that cd command can't used in scripting and so used alias. But I dont see change in directory in my terminal after running this. so IDK if it is even running the cd command. – user_rak Jul 12 '15 at 17:23
  • 1
    Are you trying to write a script or create an alias? They are different things. What you have about is an alias. It is not at all related to a script of the same name (the only connection is that they have the same name). You can run `cd` in a script but it will not change the directory of your running shell. You can write an alias which changes directory in your current shell but that's not a script. – Etan Reisner Jul 12 '15 at 18:05
  • @ Etan I want to navigate to a certain location from my current home folder and then open a new tab with that new location and now invoke a tool. All this should be done by a script. This is what I am trying to do. – user_rak Jul 12 '15 at 21:35

4 Answers4

1

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.

1

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
Qeole
  • 8,284
  • 1
  • 24
  • 52
  • let us just forget the path mentioned in the question. so I just have this in the script file cd /Desktop in my script and when i use "source script_hdl" , i get no such file or directory and when i use ". script_hdl" i get /bin/.: Permission denied. But i have the folder called Desktop in the current location. Why does it show there is no such file or directory ? – user_rak Jul 12 '15 at 18:25
  • 1
    When you call `source script_hdl`, you're sourcing the script file. If it is not in the current working directory, you must add the path, in this way: `source /example/path/to/script_hdl`. For the second error message, I think this is due to the alias you previously defined in your terminal. Try `. ./script_hdl`, it will not call the alias (or try from a new tty). – Qeole Jul 12 '15 at 18:29
0

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)

0

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