如何在shell脚本运行“CD”,呆在那里的脚本结束后? [英] How to run 'cd' in shell script and stay there after script finishes?

查看:590
本文介绍了如何在shell脚本运行“CD”,呆在那里的脚本结束后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在shell脚本中使用更改目录(庆典)

I used 'change directory' in my shell script (bash)

#!/bin/bash
alias mycd='cd some_place'
mycd
pwd

PWD 打印 some_place 正确,但脚本完成后,我当前的工作目录并没有改变。

pwd prints some_place correctly, but after the script finished my current working directory doesn't change.

是否有可能通过脚本来改变我的路径?

Is it possible to change my path by script?

推荐答案

您需要将源文件为:

. myfile.sh

source myfile.sh

没有采购的变化在子壳和未在其中调用脚本的父外壳会发生。但是,当你源文件,就好像在命令行中键入文件中的行执行。

Without sourcing the changes will happen in the sub-shell and not in the parent shell which is invoking the script. But when you source a file the lines in the file are executed as if they were typed at the command line.

这篇关于如何在shell脚本运行“CD”,呆在那里的脚本结束后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆