Why doesn't “cd” work in a shell script?

2018-12-31 01:44发布

I'm trying to write a small script to change the current directory to my project directory:

#!/bin/bash
cd /home/tree/projects/java

I saved this file as proj, added execute permission with chmod, and copied it to /usr/bin. When I call it by: proj, it does nothing. What am I doing wrong?

标签: linux shell
29条回答
孤独总比滥情好
2楼-- · 2018-12-31 02:14

You can combine an alias and a script,

alias proj="cd \`/usr/bin/proj !*\`"

provided that the script echos the destination path. Note that those are backticks surrounding the script name. 

For example, your script could be

#!/bin/bash
echo /home/askgelal/projects/java/$1

The advantage with this technique is that the script could take any number of command line parameters and emit different destinations calculated by possibly complex logic.

查看更多
回忆,回不去的记忆
3楼-- · 2018-12-31 02:16

Using Bash Profile Functions :

One feature of the bash profile is to store custom functions that can be run in the terminal or in bash scripts the same way you run application/commands this also could be used as a shortcut for long commands.

To make your function efficient system widely you will need to copy your function at the end of several files

/home/user/.bashrc
/home/user/.bash_profile
/root/.bashrc
/root/.bash_profile

You can sudo kwrite /home/user/.bashrc /home/user/.bash_profile /root/.bashrc /root/.bash_profile to edit/create those files quickly

Script Example

Making shortcut to cd .. with cdd

cdd() {
  cd ..
}

ls shortcut

ll() {
  ls -l -h
}

ls shortcut

lll() {
  ls -l -h -a
}

Howto :

Copy your function at the end of your files and reboot your terminal you can then run cdd or whatever the function you wrote

查看更多
明月照影归
4楼-- · 2018-12-31 02:17

I got my code to work by using. <your file name>

./<your file name> dose not work because it doesn't change your directory in the terminal it just changes the directory specific to that script.

Here is my program

#!/bin/bash 
echo "Taking you to eclipse's workspace."
cd /Developer/Java/workspace

Here is my terminal

nova:~ Kael$ 
nova:~ Kael$ . workspace.sh
Taking you to eclipe's workspace.
nova:workspace Kael$ 
查看更多
看淡一切
5楼-- · 2018-12-31 02:19

On my particular case i needed too many times to change for the same directory. So on my .bashrc (I use ubuntu) i've added the

1 -

$ nano ~./bashrc

 function switchp
 {
    cd /home/tree/projects/$1
 }

2-

$ source ~/.bashrc

3 -

$ switchp java

Directly it will do: cd /home/tree/projects/java

Hope that helps!

查看更多
谁念西风独自凉
6楼-- · 2018-12-31 02:19

You can combine Adam & Greg's alias and dot approaches to make something that can be more dynamic—

alias project=". project"

Now running the project alias will execute the project script in the current shell as opposed to the subshell.

查看更多
还给你的自由
7楼-- · 2018-12-31 02:20

Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The cd succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.

One way to get around this is to use an alias instead:

alias proj="cd /home/tree/projects/java"
查看更多
登录 后发表回答