首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何分辨“CD-”而不是输出“PWD”?

如何分辨“CD-”而不是输出“PWD”?
EN

Stack Overflow用户
提问于 2018-08-21 02:03:00
回答 2查看 0关注 0票数 0

在LINUX中,cd -移动到上一个工作目录并输出其新的pwd。我只想转到上一个工作目录。我不希望输出位置。怎么做?

EN

回答 2

Stack Overflow用户

发布于 2018-08-21 10:47:56

来自bash手册:

If DIRECTORY is '-', it is converted to '$OLDPWD' before the directory change is attempted. If a non-empty directory name from 'CDPATH' is used, or if '-' is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output.

这意味着可以使用$OLDPWD,而不是-。但只有它被设置:

代码语言:txt
复制
cd "${OLDPWD:--}"

为方便起见,可以创建一个helper函数:

代码语言:txt
复制
cdback() { cd "${OLDPWD:--}" ; }

或者你可以使用pushdirpopdir,像这样:

代码语言:txt
复制
pushdir /path/to/dir
    # do something
popdir # return to previous working directory
票数 0
EN

Stack Overflow用户

发布于 2018-08-21 11:51:50

可以将输出输送到dev/null:

代码语言:txt
复制
cd - > /dev/null
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档