bash 在 mac 和 linux 上使用终端有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8051145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between using the terminal on a mac vs linux?
提问by Dili
I've been using Ubuntu for the last four years.
I have a basic knowledge of shell commands and I prefer working in a terminal rather than using a GUI. Recently I've started using a Mac.
过去四年我一直在使用 Ubuntu。
我对 shell 命令有基本的了解,我更喜欢在终端中工作而不是使用 GUI。最近我开始使用Mac。
I've tried a few terminal commands (that I use on Ubuntu) in the Mac terminal and it seems to respond in mostly the same way.
我在 Mac 终端中尝试了一些终端命令(我在 Ubuntu 上使用),它似乎以大致相同的方式响应。
Are there any significant differences in the commands I use, the task(s) they perform or the shell environment that I should be aware of?
我使用的命令、它们执行的任务或我应该注意的 shell 环境是否有任何显着差异?
采纳答案by Michael Durrant
If you did a new or clean install of OS X version 10.3 or more recent, the default user terminal shell is bash.
如果您全新安装或全新安装了 OS X 10.3 或更高版本,则默认用户终端 shell 是 bash。
Bash is essentially an enhanced and GNU freeware version of the original Bourne shell, sh. If you have previous experience with bash (often the default on GNU/Linux installations), this makes the OS X command-line experience familiar, otherwise consider switching your shell either to tcsh or to zsh, as some find these more user-friendly.
Bash 本质上是原始 Bourne shell sh 的增强型 GNU 免费软件版本。如果您以前有使用 bash 的经验(通常是 GNU/Linux 安装的默认设置),这将使 OS X 命令行体验变得熟悉,否则考虑将您的 shell 切换到 tcsh 或 zsh,因为有些人发现这些对用户更友好。
If you upgraded from or use OS X version 10.2.x, 10.1.x or 10.0.x, the default user shell is tcsh, an enhanced version of csh('c-shell'). Early implementations were a bit buggy and the programming syntax a bit weird so it developed a bad rap.
如果您从 OS X 版本 10.2.x、10.1.x 或 10.0.x 升级或使用,默认用户 shell 是 tcsh,csh('c-shell') 的增强版本。早期的实现有点错误,编程语法有点奇怪,所以它发展了一个糟糕的说唱。
There are still some fundamental differences between mac and linux as Gordon Davisson so aptly lists, for example no useradd
on Mac and ifconfig
works differently.
mac 和 linux 之间仍然存在一些根本差异,正如戈登戴维森如此恰当地列出的那样,例如useradd
在 Mac 上没有,并且ifconfig
工作方式不同。
The following table is useful for knowing the various unix shells.
下表对于了解各种 unix shell 很有用。
sh The original Bourne shell Present on every unix system
ksh Original Korn shell Richer shell programming environment than sh
csh Original C-shell C-like syntax; early versions buggy
tcsh Enhanced C-shell User-friendly and less buggy csh implementation
bash GNU Bourne-again shell Enhanced and free sh implementation
zsh Z shell Enhanced, user-friendly ksh-like shell
You may also find these guides helpful:
您可能还会发现这些指南很有帮助:
http://homepage.mac.com/rgriff/files/TerminalBasics.pdf
http://homepage.mac.com/rgriff/files/TerminalBasics.pdf
http://guides.macrumors.com/Terminal
http://www.ofb.biz/safari/article/476.html
http://guides.macrumors.com/Terminal
http://www.ofb.biz/safari/article/476.html
On a final note, I am on Linux (Ubuntu 11) and Mac osX so I use bash and the thing I like the most is customizing the .bashrc (source'd from .bash_profile
on OSX) file with aliases, some examples below.
I now placed all my aliases in a separate .bash_aliases file and include it with:
最后要注意的是,我使用的是 Linux(Ubuntu 11)和 Mac osX,所以我使用 bash,我最喜欢的是.bash_profile
使用别名自定义 .bashrc(源自OSX)文件,下面是一些示例。我现在将所有别名放在一个单独的 .bash_aliases 文件中,并将其包含在:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
in the .bashrc or .bash_profile file.
在 .bashrc 或 .bash_profile 文件中。
Note that this is an example of a mac-linux difference because on a Mac you can't have the --color=auto
. The first time I did this (without knowing) I redefined ls
to be invalid which was a bit alarming until I removed --auto-color
!
请注意,这是 mac-linux 差异的一个示例,因为在 Mac 上您不能拥有--color=auto
. 我第一次这样做(不知道)我重新定义ls
为无效,这有点令人担忧,直到我删除--auto-color
!
You may also find https://unix.stackexchange.com/q/127799/10043useful
您可能还会发现https://unix.stackexchange.com/q/127799/10043很有用
# ~/.bash_aliases
# ls variants
#alias l='ls -CF'
alias la='ls -A'
alias l='ls -alFtr'
alias lsd='ls -d .*'
# Various
alias h='history | tail'
alias hg='history | grep'
alias mv='mv -i'
alias zap='rm -i'
# One letter quickies:
alias p='pwd'
alias x='exit'
alias {ack,ak}='ack-grep'
# Directories
alias s='cd ..'
alias play='cd ~/play/'
# Rails
alias src='script/rails console'
alias srs='script/rails server'
alias raked='rake db:drop db:create db:migrate db:seed'
alias rvm-restart='source '\''/home/durrantm/.rvm/scripts/rvm'\'''
alias rrg='rake routes | grep '
alias rspecd='rspec --drb '
#
# DropBox - syncd
WORKBASE="~/Dropbox/97_2012/work"
alias work="cd $WORKBASE"
alias code="cd $WORKBASE/ror/code"
#
# DropNot - NOT syncd !
WORKBASE_GIT="~/Dropnot"
alias {dropnot,not}="cd $WORKBASE_GIT"
alias {webs,ww}="cd $WORKBASE_GIT/webs"
alias {setups,docs}="cd $WORKBASE_GIT/setups_and_docs"
alias {linker,lnk}="cd $WORKBASE_GIT/webs/rails_v3/linker"
#
# git
alias {gsta,gst}='git status'
# Warning: gst conflicts with gnu-smalltalk (when used).
alias {gbra,gb}='git branch'
alias {gco,go}='git checkout'
alias {gcob,gob}='git checkout -b '
alias {gadd,ga}='git add '
alias {gcom,gc}='git commit'
alias {gpul,gl}='git pull '
alias {gpus,gh}='git push '
alias glom='git pull origin master'
alias ghom='git push origin master'
alias gg='git grep '
#
# vim
alias v='vim'
#
# tmux
alias {ton,tn}='tmux set -g mode-mouse on'
alias {tof,tf}='tmux set -g mode-mouse off'
#
# dmc
alias {dmc,dm}='cd ~/Dropnot/webs/rails_v3/dmc/'
alias wf='cd ~/Dropnot/webs/rails_v3/dmc/dmWorkflow'
alias ws='cd ~/Dropnot/webs/rails_v3/dmc/dmStaffing'
回答by Gordon Davisson
@Michael Durrant's answer ably covers the shell itself, but the shell environment also includes the various commands you use in the shell and these are going to be similar -- but not identical -- between OS X and linux. In general, both will have the same core commands and features (especially those defined in the Posix standard), but a lot of extensions will be different.
@Michael Durrant 的回答巧妙地涵盖了 shell 本身,但 shell 环境还包括您在 shell 中使用的各种命令,这些命令在 OS X 和 linux 之间将是相似的——但不完全相同。通常,两者都具有相同的核心命令和功能(尤其是 Posix 标准中定义的那些),但很多扩展会有所不同。
For example, linux systems generally have a useradd
command to create new users, but OS X doesn't. On OS X, you generally use the GUI to create users; if you need to create them from the command line, you use dscl
(which linux doesn't have) to edit the user database (see here). (Update: starting in macOS High Sierra v10.13, you can use sysadminctl -addUser
instead.)
例如,Linux 系统通常有useradd
创建新用户的命令,但 OS X 没有。在 OS X 上,您通常使用 GUI 创建用户;如果您需要从命令行创建它们,您可以使用dscl
(Linux 没有)来编辑用户数据库(请参阅此处)。(更新:从 macOS High Sierra v10.13 开始,您可以sysadminctl -addUser
改用。)
Also, some commands they have in common will have different features and options. For example, linuxes generally include GNU sed
, which uses the -r
option to invoke extended regular expressions; on OS X, you'd use the -E
option to get the same effect. Similarly, in linux you might use ls --color=auto
to get colorized output; on macOS, the closest equivalent is ls -G
.
此外,它们共有的一些命令将具有不同的功能和选项。例如,Linux 系统通常包含 GNU sed
,它使用-r
调用扩展正则表达式的选项;在 OS X 上,您可以使用该-E
选项来获得相同的效果。同样,在 linux 中,您可能会使用它ls --color=auto
来获取彩色输出;在 macOS 上,最接近的等价物是ls -G
.
EDIT: Another difference is that many linux commands allow options to be specified after their arguments (e.g. ls file1 file2 -l
), while most OS X commands require options to come strictly first (ls -l file1 file2
).
编辑:另一个区别是许多 linux 命令允许在它们的参数之后指定选项(例如ls file1 file2 -l
),而大多数 OS X 命令要求选项严格首先出现(ls -l file1 file2
)。
Finally, since the OS itself is different, some commands wind up behaving differently between the OSes. For example, on linux you'd probably use ifconfig
to change your network configuration. On OS X, ifconfig
will work (probably with slightly different syntax), but your changes are likely to be overwritten randomly by the system configuration daemon; instead you should edit the network preferences with networksetup
, and then let the config daemon apply them to the live network state.
最后,由于操作系统本身不同,某些命令在操作系统之间的行为会有所不同。例如,在 linux 上,您可能会ifconfig
用来更改网络配置。在 OS X 上,ifconfig
可以工作(可能语法略有不同),但您的更改可能会被系统配置守护程序随机覆盖;相反,您应该使用 编辑网络首选项networksetup
,然后让配置守护程序将它们应用于实时网络状态。