为什么“cd”不能在shell脚本中工作?

时间:2020-12-17 01:14:19

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?

我将此文件保存为proj,并添加了chmod的execute权限,并将其复制到/usr/bin。当我叫它proj时,它什么也不做。我做错了什么?

27 个解决方案

#1


482  

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.

Shell脚本在子Shell中运行,每个子Shell都有自己的当前目录的概念。cd成功了,但是当子shell退出时,您又回到了交互式shell中,并且没有任何更改。

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

解决这个问题的一种方法是使用别名:

alias proj="cd /home/tree/projects/java"

#2


389  

You're doing nothing wrong! You've changed the directory, but only within the subshell that runs the script.

你做的没有错!您已经更改了目录,但只更改了运行脚本的子shell。

You can run the script in your current process with the "dot" command:

您可以使用“dot”命令在当前流程中运行该脚本:

. proj

But I'd prefer Greg's suggestion to use an alias in this simple case.

但是我更喜欢Greg的建议,在这个简单的例子中使用一个别名。

#3


186  

The cd in your script technically worked as it changed the directory of the shell that ran the script, but that was a separate process forked from your interactive shell.

您的脚本中的cd在技术上是工作的,因为它改变了运行脚本的shell的目录,但是这是一个单独的过程,从您的交互式shell中分离出来。

A Posix-compatible way to solve this problem is to define a shell procedure rather than a shell-invoked command script.

解决这个问题的一种与posix兼容的方法是定义一个shell过程,而不是一个shell调用的命令脚本。

jhome () {
  cd /home/tree/projects/java
}

You can just type this in or put it in one of the various shell startup files.

您可以将其输入或放入不同的shell启动文件中。

#4


116  

The cd is done within the script's shell. When the script ends, that shell exits, and then you are left in the directory you were. "Source" the script, don't run it. Instead of:

cd在脚本的shell中完成。当脚本结束时,该shell将退出,然后您将被保留在您所在的目录中。“源”脚本,不要运行它。而不是:

./myscript.sh

do

. ./myscript.sh

(Notice the dot and space before the script name.)

(注意脚本名称前面的点和空格。)

#5


89  

To make a bash script that will cd to a select directory :

Create the script file

创建一个脚本文件

#!/bin/sh
# file : /scripts/cdjava
#
cd /home/askgelal/projects/java

Then create an alias in your startup file.

然后在启动文件中创建一个别名。

#!/bin/sh
# file /scripts/mastercode.sh
#
alias cdjava='. /scripts/cdjava'

  • I created a startup file where I dump all my aliases and custom functions.
  • 我创建了一个启动文件,将所有别名和自定义函数转储到其中。
  • Then I source this file into my .bashrc to have it set on each boot.
  • 然后,我将这个文件发送到我的.bashrc中,以便将它设置在每个引导上。

For example, create a master aliases/functions file: /scripts/mastercode.sh
(Put the alias in this file.)

例如,创建一个主别名/函数文件:/脚本/主代码。sh(将别名放在此文件中)。

Then at the end of your .bashrc file:

然后在。bashrc文件的末尾:

source /scripts/mastercode.sh



Now its easy to cd to your java directory, just type cdjava and you are there.

现在,cd到java目录很容易,只要键入cdjava就可以了。

#6


36  

Jeremy Ruten's idea of using a symlink triggered a thought that hasn't crossed any other answer. Use:

杰里米·鲁顿关于使用符号链接的想法引发了一种从未有过任何其他答案的想法。使用:

CDPATH=:$HOME/projects

The leading colon is important; it means that if there is a directory 'dir' in the current directory, then 'cd dir' will change to that, rather than hopping off somewhere else. With the value set as shown, you can do:

主结肠很重要;这意味着,如果当前目录中有一个目录“dir”,那么“cd dir”将更改为该目录,而不是跳到其他地方。使用如图所示的值,您可以:

cd java

and, if there is no sub-directory called java in the current directory, then it will take you directly to $HOME/projects/java - no aliases, no scripts, no dubious execs or dot commands.

而且,如果当前目录中没有名为java的子目录,那么它将直接带您到$HOME/projects/java—没有别名、没有脚本、没有可疑的execs或dot命令。

My $HOME is /Users/jleffler; my $CDPATH is:

我的$ HOME /用户/ jleffler;我的CDPATH美元是:

:/Users/jleffler:/Users/jleffler/mail:/Users/jleffler/src:/Users/jleffler/src/perl:/Users/jleffler/src/sqltools:/Users/jleffler/lib:/Users/jleffler/doc:/Users/jleffler/work

#7


24  

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$ 

#8


20  

you can use

您可以使用

. script_name

。script_name

to execute it, it won't execute in subshell.

要执行它,它不会在子shell中执行。

#9


14  

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.

bash脚本在其当前环境或其子元素上运行,但从不在其父环境中运行。

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

但是,这个问题经常被问到,因为在从另一个目录执行bash脚本之后,希望在某个目录中留下一个bash提示符。

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

如果是这样,只需在脚本末尾执行一个child bash实例:

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

#10


13  

simply run:

简单地运行:

cd /home/xxx/yyy && command_you_want

#11


11  

When you fire a shell script, it runs a new instance of that shell (/bin/bash). Thus, your script just fires up a shell, changes the directory and exits. Put another way, cd (and other such commands) within a shell script do not affect nor have access to the shell from which they were launched.

当您启动shell脚本时,它将运行该shell的一个新实例(/bin/bash)。因此,您的脚本只是启动一个shell,更改目录并退出。换句话说,在shell脚本中,cd(和其他此类命令)不会影响或访问它们启动的shell。

#12


9  

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

在我的特殊情况下,我需要多次更改相同的目录。在我的。bashrc(我使用ubuntu)中添加了。

1 -

1 -

$ nano ~./bashrc

纳米~。美元/ bashrc。

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

2-

2 -

$ source ~/.bashrc

$ ~ / . bashrc来源

3 -

3 -

$ switchp java

美元switchp java

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

它将直接执行:cd /home/tree/projects/java。

Hope that helps!

希望会有帮助!

#13


7  

It only changes the directory for the script itself, while your current directory stays the same.

它只会为脚本本身更改目录,而当前目录保持不变。

You might want to use a symbolic link instead. It allows you to make a "shortcut" to a file or directory, so you'd only have to type something like cd my-project.

您可能想要使用一个符号链接。它允许您为文件或目录创建一个“快捷方式”,因此您只需要键入类似cd my-project的东西。

#14


7  

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.

这种技术的优点是,脚本可以使用任意数量的命令行参数,并根据可能复杂的逻辑计算出不同的目的地。

#15


6  

You can do following:

你可以做以下:

#!/bin/bash
cd /your/project/directory
# start another shell and replacing the current
exec /bin/bash

EDIT: This could be 'dotted' as well, to prevent creation of subsequent shells.

编辑:这也可以是“点”,以防止产生后续的shell。

Example:

例子:

. ./previous_script  (with or without the first line)

#16


5  

to navigate directories quicky, there's $CDPATH, cdargs, and ways to generate aliases automatically

为了快速导航目录,有$CDPATH、cdargs和自动生成别名的方法。

http://jackndempsey.blogspot.com/2008/07/cdargs.html

http://jackndempsey.blogspot.com/2008/07/cdargs.html

http://muness.blogspot.com/2008/06/lazy-bash-cd-aliaes.html

http://muness.blogspot.com/2008/06/lazy-bash-cd-aliaes.html

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5827311.html

https://web.archive.org/web/1/http:/ / articles.techrepublic % 2易康姆% 2易康姆/ 5100 - 10878 - _11 - 5827311. - html

#17


4  

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

你可以把Adam & Greg的别名和dot方法结合起来,使之更有活力。

alias project=". project"

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

现在运行项目别名将在当前shell中执行项目脚本,而不是subshell。

#18


3  

You can use the operator && :

你可以使用接线员&&:

cd myDirectory && ls

cd myDirectory & & ls

#19


3  

In your ~/.bash_profile file. add the next function

在~ /。bash_profile文件。添加下一个函数

move_me() {
    cd ~/path/to/dest
}

Restart terminal and you can type

重新启动终端,你可以打字。

move_me 

and you will be moved to the destination folder.

然后您将被移动到目标文件夹。

#20


2  

LOOOOOng time after, but I did the following:

之后的时间,我做了以下的事情:

create a file called case

创建一个名为case的文件。

paste the following in the file:

在文件中粘贴以下内容:

#!/bin/sh

cd /home/"$1"

save it and then:

保存它,然后:

chmod +x case

I also created an alias in my .bashrc:

我还在.bashrc中创建了一个别名:

alias disk='cd /home/; . case'

now when I type:

现在当我类型:

case 12345

essentially I am typing:

基本上我打字:

cd /home/12345

You can type any folder after 'case':

您可以在“案例”后键入任何文件夹:

case 12

case 15

case 17

which is like typing:

就像打字:

cd /home/12

cd /home/15

cd /home/17

respectively

分别

In my case the path is much longer - these guys summed it up with the ~ info earlier.

在我的例子中,路径要长得多——这些家伙在前面总结了~信息。

#21


2  

While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.

在寻找您想要运行的脚本是一个解决方案时,您应该知道这个脚本可以直接修改当前shell的环境。也不可能再通过争论了。

Another way to do, is to implement your script as a function in bash.

另一种方法是在bash中实现脚本作为函数。

function cdbm() {
  cd whereever_you_want_to_go
  echo "Arguments to the functions were $1, $2, ..."
}

This technique is used by autojump: http://github.com/joelthelion/autojump/wiki to provide you with learning shell directory bookmarks.

这个技术被autojump使用:http://github.com/joelthelion/autojump/wiki为您提供学习shell目录书签。

#22


1  

You can create a function like below in your .bash_profile and it will work smoothly.

您可以在.bash_profile中创建一个类似于下面的函数,它将会很顺利地工作。

The following function takes an optional parameter which is a project. For example, you can just run

下面的函数接受一个可选参数,它是一个项目。例如,您可以运行。

cdproj

or

cdproj project_name

Here is the function definition.

这是函数定义。

cdproj(){
    dir=/Users/yourname/projects
    if [ "$1" ]; then
      cd "${dir}/${1}"
    else
      cd "${dir}"
    fi
}

Dont forget to source your .bash_profile

不要忘记输入.bash_profile。

#23


0  

If you are using fish as your shell, the best solution is to create a function. As an example, given the original question, you could copy the 4 lines below and paste them into your fish command line:

如果您将鱼作为您的外壳,最好的解决方案是创建一个函数。作为一个例子,考虑到最初的问题,您可以复制下面的4行,并将它们粘贴到您的fish命令行中:

function proj
   cd /home/tree/projects/java
end
funcsave proj

This will create the function and save it for use later. If your project changes, just repeat the process using the new path.

这将创建函数并将其保存以便以后使用。如果您的项目更改了,只需使用新路径重复此过程。

If you prefer, you can manually add the function file by doing the following:

如果您愿意,您可以手动添加函数文件:

nano ~/.config/fish/functions/proj.fish

and enter the text:

输入文本:

function proj
   cd /home/tree/projects/java
end

and finally press ctrl+x to exit and y followed by return to save your changes.

最后按下ctrl+x退出,然后y返回来保存更改。

(NOTE: the first method of using funcsave creates the proj.fish file for you).

(注意:使用funcsave的第一种方法创建proj。鱼文件给你)。

#24


0  

I have a simple bash script called p to manage directory changing on
github.com/godzilla/bash-stuff
just put the script in your local bin directory (/usr/local/bin)
and put

我有一个简单的bash脚本,叫做p,用来管理github.com/godzilla/bash-stuff上的目录更改,只需将脚本放入本地bin目录(/usr/local/bin),然后放入。

alias p='. p'

in your .bashrc

. bashrc中

#25


0  

You need no script, only set the correct option and create an environment variable.

您无需脚本,只需设置正确的选项并创建一个环境变量。

shopt -s cdable_vars

in your ~/.bashrc allows to cd to the content of environment variables.

在~ /。bashrc允许cd到环境变量的内容。

Create such an environment variable:

创建这样一个环境变量:

export myjava="/home/tree/projects/java"

and you can use:

你可以使用:

cd myjava

Other alternatives.

其他选择。

#26


0  

Using Bash Profile Functions :

使用Bash概要文件功能:

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.

bash概要文件的一个特性是存储可以在终端中运行的自定义函数,或者在bash脚本中使用与运行应用程序/命令相同的方式,这也可以作为长命令的快捷方式。

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

您可以选择kwrite /home/ user/。bashrc /home/user/.(bash_profile /root/.bashrc /root/.(bash_profile可以快速编辑/创建这些文件。

Script Example

脚本的例子

Making shortcut to cd .. with cdd

抄写cd的快捷方式。和cdd

cdd() {
  cd ..
}

ls shortcut

ls的快捷方式

ll() {
  ls -l -h
}

ls shortcut

ls的快捷方式

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

Howto :

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

将您的函数复制到文件的末尾,并重新启动终端,您就可以运行cdd或任何您所编写的函数。

#27


-1  

You can execute some lines in the same subshell if you end lines with backslash.

如果使用反斜杠结束行,可以在相同的子shell中执行一些行。

cd somedir; \
pwd

#1


482  

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.

Shell脚本在子Shell中运行,每个子Shell都有自己的当前目录的概念。cd成功了,但是当子shell退出时,您又回到了交互式shell中,并且没有任何更改。

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

解决这个问题的一种方法是使用别名:

alias proj="cd /home/tree/projects/java"

#2


389  

You're doing nothing wrong! You've changed the directory, but only within the subshell that runs the script.

你做的没有错!您已经更改了目录,但只更改了运行脚本的子shell。

You can run the script in your current process with the "dot" command:

您可以使用“dot”命令在当前流程中运行该脚本:

. proj

But I'd prefer Greg's suggestion to use an alias in this simple case.

但是我更喜欢Greg的建议,在这个简单的例子中使用一个别名。

#3


186  

The cd in your script technically worked as it changed the directory of the shell that ran the script, but that was a separate process forked from your interactive shell.

您的脚本中的cd在技术上是工作的,因为它改变了运行脚本的shell的目录,但是这是一个单独的过程,从您的交互式shell中分离出来。

A Posix-compatible way to solve this problem is to define a shell procedure rather than a shell-invoked command script.

解决这个问题的一种与posix兼容的方法是定义一个shell过程,而不是一个shell调用的命令脚本。

jhome () {
  cd /home/tree/projects/java
}

You can just type this in or put it in one of the various shell startup files.

您可以将其输入或放入不同的shell启动文件中。

#4


116  

The cd is done within the script's shell. When the script ends, that shell exits, and then you are left in the directory you were. "Source" the script, don't run it. Instead of:

cd在脚本的shell中完成。当脚本结束时,该shell将退出,然后您将被保留在您所在的目录中。“源”脚本,不要运行它。而不是:

./myscript.sh

do

. ./myscript.sh

(Notice the dot and space before the script name.)

(注意脚本名称前面的点和空格。)

#5


89  

To make a bash script that will cd to a select directory :

Create the script file

创建一个脚本文件

#!/bin/sh
# file : /scripts/cdjava
#
cd /home/askgelal/projects/java

Then create an alias in your startup file.

然后在启动文件中创建一个别名。

#!/bin/sh
# file /scripts/mastercode.sh
#
alias cdjava='. /scripts/cdjava'

  • I created a startup file where I dump all my aliases and custom functions.
  • 我创建了一个启动文件,将所有别名和自定义函数转储到其中。
  • Then I source this file into my .bashrc to have it set on each boot.
  • 然后,我将这个文件发送到我的.bashrc中,以便将它设置在每个引导上。

For example, create a master aliases/functions file: /scripts/mastercode.sh
(Put the alias in this file.)

例如,创建一个主别名/函数文件:/脚本/主代码。sh(将别名放在此文件中)。

Then at the end of your .bashrc file:

然后在。bashrc文件的末尾:

source /scripts/mastercode.sh



Now its easy to cd to your java directory, just type cdjava and you are there.

现在,cd到java目录很容易,只要键入cdjava就可以了。

#6


36  

Jeremy Ruten's idea of using a symlink triggered a thought that hasn't crossed any other answer. Use:

杰里米·鲁顿关于使用符号链接的想法引发了一种从未有过任何其他答案的想法。使用:

CDPATH=:$HOME/projects

The leading colon is important; it means that if there is a directory 'dir' in the current directory, then 'cd dir' will change to that, rather than hopping off somewhere else. With the value set as shown, you can do:

主结肠很重要;这意味着,如果当前目录中有一个目录“dir”,那么“cd dir”将更改为该目录,而不是跳到其他地方。使用如图所示的值,您可以:

cd java

and, if there is no sub-directory called java in the current directory, then it will take you directly to $HOME/projects/java - no aliases, no scripts, no dubious execs or dot commands.

而且,如果当前目录中没有名为java的子目录,那么它将直接带您到$HOME/projects/java—没有别名、没有脚本、没有可疑的execs或dot命令。

My $HOME is /Users/jleffler; my $CDPATH is:

我的$ HOME /用户/ jleffler;我的CDPATH美元是:

:/Users/jleffler:/Users/jleffler/mail:/Users/jleffler/src:/Users/jleffler/src/perl:/Users/jleffler/src/sqltools:/Users/jleffler/lib:/Users/jleffler/doc:/Users/jleffler/work

#7


24  

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$ 

#8


20  

you can use

您可以使用

. script_name

。script_name

to execute it, it won't execute in subshell.

要执行它,它不会在子shell中执行。

#9


14  

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.

bash脚本在其当前环境或其子元素上运行,但从不在其父环境中运行。

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

但是,这个问题经常被问到,因为在从另一个目录执行bash脚本之后,希望在某个目录中留下一个bash提示符。

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

如果是这样,只需在脚本末尾执行一个child bash实例:

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

#10


13  

simply run:

简单地运行:

cd /home/xxx/yyy && command_you_want

#11


11  

When you fire a shell script, it runs a new instance of that shell (/bin/bash). Thus, your script just fires up a shell, changes the directory and exits. Put another way, cd (and other such commands) within a shell script do not affect nor have access to the shell from which they were launched.

当您启动shell脚本时,它将运行该shell的一个新实例(/bin/bash)。因此,您的脚本只是启动一个shell,更改目录并退出。换句话说,在shell脚本中,cd(和其他此类命令)不会影响或访问它们启动的shell。

#12


9  

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

在我的特殊情况下,我需要多次更改相同的目录。在我的。bashrc(我使用ubuntu)中添加了。

1 -

1 -

$ nano ~./bashrc

纳米~。美元/ bashrc。

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

2-

2 -

$ source ~/.bashrc

$ ~ / . bashrc来源

3 -

3 -

$ switchp java

美元switchp java

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

它将直接执行:cd /home/tree/projects/java。

Hope that helps!

希望会有帮助!

#13


7  

It only changes the directory for the script itself, while your current directory stays the same.

它只会为脚本本身更改目录,而当前目录保持不变。

You might want to use a symbolic link instead. It allows you to make a "shortcut" to a file or directory, so you'd only have to type something like cd my-project.

您可能想要使用一个符号链接。它允许您为文件或目录创建一个“快捷方式”,因此您只需要键入类似cd my-project的东西。

#14


7  

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.

这种技术的优点是,脚本可以使用任意数量的命令行参数,并根据可能复杂的逻辑计算出不同的目的地。

#15


6  

You can do following:

你可以做以下:

#!/bin/bash
cd /your/project/directory
# start another shell and replacing the current
exec /bin/bash

EDIT: This could be 'dotted' as well, to prevent creation of subsequent shells.

编辑:这也可以是“点”,以防止产生后续的shell。

Example:

例子:

. ./previous_script  (with or without the first line)

#16


5  

to navigate directories quicky, there's $CDPATH, cdargs, and ways to generate aliases automatically

为了快速导航目录,有$CDPATH、cdargs和自动生成别名的方法。

http://jackndempsey.blogspot.com/2008/07/cdargs.html

http://jackndempsey.blogspot.com/2008/07/cdargs.html

http://muness.blogspot.com/2008/06/lazy-bash-cd-aliaes.html

http://muness.blogspot.com/2008/06/lazy-bash-cd-aliaes.html

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5827311.html

https://web.archive.org/web/1/http:/ / articles.techrepublic % 2易康姆% 2易康姆/ 5100 - 10878 - _11 - 5827311. - html

#17


4  

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

你可以把Adam & Greg的别名和dot方法结合起来,使之更有活力。

alias project=". project"

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

现在运行项目别名将在当前shell中执行项目脚本,而不是subshell。

#18


3  

You can use the operator && :

你可以使用接线员&&:

cd myDirectory && ls

cd myDirectory & & ls

#19


3  

In your ~/.bash_profile file. add the next function

在~ /。bash_profile文件。添加下一个函数

move_me() {
    cd ~/path/to/dest
}

Restart terminal and you can type

重新启动终端,你可以打字。

move_me 

and you will be moved to the destination folder.

然后您将被移动到目标文件夹。

#20


2  

LOOOOOng time after, but I did the following:

之后的时间,我做了以下的事情:

create a file called case

创建一个名为case的文件。

paste the following in the file:

在文件中粘贴以下内容:

#!/bin/sh

cd /home/"$1"

save it and then:

保存它,然后:

chmod +x case

I also created an alias in my .bashrc:

我还在.bashrc中创建了一个别名:

alias disk='cd /home/; . case'

now when I type:

现在当我类型:

case 12345

essentially I am typing:

基本上我打字:

cd /home/12345

You can type any folder after 'case':

您可以在“案例”后键入任何文件夹:

case 12

case 15

case 17

which is like typing:

就像打字:

cd /home/12

cd /home/15

cd /home/17

respectively

分别

In my case the path is much longer - these guys summed it up with the ~ info earlier.

在我的例子中,路径要长得多——这些家伙在前面总结了~信息。

#21


2  

While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.

在寻找您想要运行的脚本是一个解决方案时,您应该知道这个脚本可以直接修改当前shell的环境。也不可能再通过争论了。

Another way to do, is to implement your script as a function in bash.

另一种方法是在bash中实现脚本作为函数。

function cdbm() {
  cd whereever_you_want_to_go
  echo "Arguments to the functions were $1, $2, ..."
}

This technique is used by autojump: http://github.com/joelthelion/autojump/wiki to provide you with learning shell directory bookmarks.

这个技术被autojump使用:http://github.com/joelthelion/autojump/wiki为您提供学习shell目录书签。

#22


1  

You can create a function like below in your .bash_profile and it will work smoothly.

您可以在.bash_profile中创建一个类似于下面的函数,它将会很顺利地工作。

The following function takes an optional parameter which is a project. For example, you can just run

下面的函数接受一个可选参数,它是一个项目。例如,您可以运行。

cdproj

or

cdproj project_name

Here is the function definition.

这是函数定义。

cdproj(){
    dir=/Users/yourname/projects
    if [ "$1" ]; then
      cd "${dir}/${1}"
    else
      cd "${dir}"
    fi
}

Dont forget to source your .bash_profile

不要忘记输入.bash_profile。

#23


0  

If you are using fish as your shell, the best solution is to create a function. As an example, given the original question, you could copy the 4 lines below and paste them into your fish command line:

如果您将鱼作为您的外壳,最好的解决方案是创建一个函数。作为一个例子,考虑到最初的问题,您可以复制下面的4行,并将它们粘贴到您的fish命令行中:

function proj
   cd /home/tree/projects/java
end
funcsave proj

This will create the function and save it for use later. If your project changes, just repeat the process using the new path.

这将创建函数并将其保存以便以后使用。如果您的项目更改了,只需使用新路径重复此过程。

If you prefer, you can manually add the function file by doing the following:

如果您愿意,您可以手动添加函数文件:

nano ~/.config/fish/functions/proj.fish

and enter the text:

输入文本:

function proj
   cd /home/tree/projects/java
end

and finally press ctrl+x to exit and y followed by return to save your changes.

最后按下ctrl+x退出,然后y返回来保存更改。

(NOTE: the first method of using funcsave creates the proj.fish file for you).

(注意:使用funcsave的第一种方法创建proj。鱼文件给你)。

#24


0  

I have a simple bash script called p to manage directory changing on
github.com/godzilla/bash-stuff
just put the script in your local bin directory (/usr/local/bin)
and put

我有一个简单的bash脚本,叫做p,用来管理github.com/godzilla/bash-stuff上的目录更改,只需将脚本放入本地bin目录(/usr/local/bin),然后放入。

alias p='. p'

in your .bashrc

. bashrc中

#25


0  

You need no script, only set the correct option and create an environment variable.

您无需脚本,只需设置正确的选项并创建一个环境变量。

shopt -s cdable_vars

in your ~/.bashrc allows to cd to the content of environment variables.

在~ /。bashrc允许cd到环境变量的内容。

Create such an environment variable:

创建这样一个环境变量:

export myjava="/home/tree/projects/java"

and you can use:

你可以使用:

cd myjava

Other alternatives.

其他选择。

#26


0  

Using Bash Profile Functions :

使用Bash概要文件功能:

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.

bash概要文件的一个特性是存储可以在终端中运行的自定义函数,或者在bash脚本中使用与运行应用程序/命令相同的方式,这也可以作为长命令的快捷方式。

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

您可以选择kwrite /home/ user/。bashrc /home/user/.(bash_profile /root/.bashrc /root/.(bash_profile可以快速编辑/创建这些文件。

Script Example

脚本的例子

Making shortcut to cd .. with cdd

抄写cd的快捷方式。和cdd

cdd() {
  cd ..
}

ls shortcut

ls的快捷方式

ll() {
  ls -l -h
}

ls shortcut

ls的快捷方式

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

Howto :

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

将您的函数复制到文件的末尾,并重新启动终端,您就可以运行cdd或任何您所编写的函数。

#27


-1  

You can execute some lines in the same subshell if you end lines with backslash.

如果使用反斜杠结束行,可以在相同的子shell中执行一些行。

cd somedir; \
pwd