尝试建立永久别名-UNIX [英] Trying to make a permanent Alias - UNIX

查看:73
本文介绍了尝试建立永久别名-UNIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Unix中创建一个永久别名( alias homedir ='cd/export/home/Files/myName').

我试图在〜/.bashrc文件中添加命令,但是在$ HOME目录中找不到该文件.唯一的bash文件是.bash_history,请帮助.

我什至执行了 ls -a ,但仍未在我的$ HOME目录中找到它.

解决方案

我建议不要将别名直接放在〜/.profile中(在注释中建议).〜/.profile文件不特定于bash.

您应该将别名放在〜/.bashrc中.

为什么?

  1. .bashrc被保证特定于bash(或其至少将来的任何变体)
  2. 如果正确设置所有内容,则所有外壳程序都可以遵循类似的约定.因此,您可以拥有.bashrc,.zshrc,.tcshrc,.kshrc等.

    • 如果您像我一样喜欢玩所有不同的外壳,您会发现这非常非常有用

如何设置

将以下代码放入〜/.profile.

 #(如果正在运行bash)如果[-n"$ BASH_VERSION"];然后#包括.bashrc(如果存在)如果[-f"$ HOME/.bashrc"];然后."$ HOME/.bashrc"科幻科幻 

这将确保〜/.bashrc在且仅当您使用bash时运行.所以最终,涉及到〜/.profile ;但您仍然可以从所有特定于bash的文件中拥有所有别名(以及其他任何特定于bash的命令)中受益.您需要将其添加到.profile的原因在这里进行了解释: bash的不同脚本之间有什么区别?./p>

然后将您的别名和其他命令添加到〜/.bashrc.如果〜/.bashrc还不存在,只需使用 touch〜/.bashrc vi〜/.bashrc 创建它.

记住,每当您修改〜/.bashrc时,都要重新应用它.否则,您将看不到更改.为此,请运行以下代码:

 源〜/.bashrc 

I am trying to make a permanent alias (alias homedir='cd /export/home/Files/myName') in unix.

I am trying to add the command in the ~/.bashrc file, but I cannot find the file in my $HOME directory. The only bash file is see is .bash_history, Please help.

I even did a ls -a and still did not find it in my $HOME directory.

解决方案

I would advise against placing your aliases directly in ~/.profile (which was suggested in the comments). The ~/.profile file is not specific to bash.

You should instead place your aliases in ~/.bashrc.

Why?

  1. .bashrc is guaranteed to be specific to bash (or at least any future variant of it)
  2. If you set everything up correctly, all of your shells can follow a similar convention. So you can have .bashrc, .zshrc, .tcshrc, .kshrc, etc.

    • If you're like me and enjoy playing around with all the different shells, you will find this very very helpful

How to Set It Up

Place the following code in ~/.profile.

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

This will ensure that ~/.bashrc runs if and only if you're using bash. So ultimately, ~/.profile is involved; but you still benefit from having all your aliases (and any other bash-specific commands) in a bash-specific file. The reason you need to add this to .profile is explained here: What's The difference between the different scripts for bash?.

Then add your aliases and other commands to ~/.bashrc. If ~/.bashrc does not already exist, just create it either with touch ~/.bashrc or vi ~/.bashrc.

Remember to re-apply ~/.bashrc whenever you modify it. Otherwise, you won't see the changes. To do that, run this code:

source ~/.bashrc

这篇关于尝试建立永久别名-UNIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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