从这里对我的另一个问题的答案中,我了解到了在 Bash 中将命令行参数传递给别名函数的可能性。
鱼,我可以通过编辑文件编辑别名config.fish在~/.config/fish目录中添加这样一行
alias lsp='ls -ah --color=always | less -R;'
Run Code Online (Sandbox Code Playgroud)
它完美无缺。这应该相当于~/.bash_aliases在 bash 中编辑
但是当我尝试设置别名函数来传递这样的参数时
alias lsp='_(){ ls -ah --color=always $* | less -R; }; _'
Run Code Online (Sandbox Code Playgroud)
它对鱼不起作用?
在设置别名以传递命令行参数的方式上,fish 和 bash 之间是否存在任何差异,以防止第二个别名与 fish 而不是 bash 一起使用?
我在 Ubuntu 12.04 LTS 下。
我已将这样的行添加到 ~/.bashrc
alias myconf="sudo nano /opt/nginx/conf/sites-available/efiling"
Run Code Online (Sandbox Code Playgroud)
我也试过单引号:
alias myconf='sudo nano /opt/nginx/conf/sites-available/efiling'
Run Code Online (Sandbox Code Playgroud)
然后我注销并再次登录。
test@STORK:~$ myconf
myconf: command not found
Run Code Online (Sandbox Code Playgroud)
但是在test@STORK:~$ source ~/.bashrc它起作用之后。
谁能帮我?
我想为 grep 创建一个别名,如下所示:
grep argX ~/myfile
Run Code Online (Sandbox Code Playgroud)
其中 argX 是参数,而 myfile 始终相同。我怎样才能做到这一点?
我经常需要输入:
cd /home/geo/Geant4/geant4.10.00.p02-install/lib/Geant4-10.0.2
Run Code Online (Sandbox Code Playgroud)
如何为该目录创建别名,以便我可以输入:
cd $geant (or any other word I decide to create as the alias name)
Run Code Online (Sandbox Code Playgroud)
它也会在我需要使用时帮助我,cmake因为我也可以$geant在cmake选项中间使用。
谢谢。
我需要为我的系统执行一个命令来搜索新的 SD 卡并能够执行它我需要以 root 身份执行它。简单地将 sudo 与命令一起使用是行不通的。这不是如何执行具有 sudo 权限的别名的问题!
我想知道如何为 root 用户添加别名,以便在使用sudo -i. 我尝试登录到 root 用户并添加别名(工作正常)。唯一的问题是我从终端注销后别名消失了。
我希望每次打开 shell 时都可以使用以下别名:
export ZONE="us-eastern1-c"
export INSTANCE_NAME="myInstance"
alias gc='gcloud compute ssh --zone=$ZONE jupyter@$INSTANCE_NAME -- -L 8080:localhost:8080'
Run Code Online (Sandbox Code Playgroud)
我应该把它放进去.bashrc还是.profile?另外,扫描.bashrc我发现:
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Run Code Online (Sandbox Code Playgroud)
那么,这里的最佳做法是什么?我应该将别名(和所需的环境变量)放在第三个文件中.bash_aliases吗?我不是配置文件扩散的忠实粉丝,但如果使用这.bash_aliases是最佳实践,我会这样做。
我创建了一个这个文件: /etc/profile.d/pycharm_alias.sh
startPyCharm() {...
}
alias py=startPyCharm
Run Code Online (Sandbox Code Playgroud)
我重新启动了 PC,但是别名不可用:
===> alias
...empty
Run Code Online (Sandbox Code Playgroud)
如果我通过 ssh 登录到 PC,则别名可用:
===> ssh root@localhost
..
root@pc:~# alias
alias py='startPyCharm'
Run Code Online (Sandbox Code Playgroud)
如何为 ubuntu linux 系统的所有用户提供别名?
我们使用 bash shell。
我试过
alias sct='systemctl'
complete -F _systemctl sct
Run Code Online (Sandbox Code Playgroud)
但是直到我在会话中运行原始命令 systemctl 后才找到函数 _systemctl 。该函数动态或以某种方式加载,并且内部包含许多其他相同的函数。
操作系统 - Ubuntu 20.04
在同一行中定义和使用别名似乎不起作用:
$ alias x=ls; x
x: command not found
Run Code Online (Sandbox Code Playgroud)
如何x在第二次调用中“转义”以便将其识别为别名?
我已经读过这篇关于在哪里放置别名的帖子。
现在,假设我的自定义命令非常复杂(在输入中接受参数,由几个相继运行的命令组成,为了清楚起见,我想将其保留为多行,涉及引号、双引号等),所以我想将它定义为一个函数,例如
sshdev_system_loop () {
while true; do
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Trying to log into ststem $2 as user $1 ."
timeout 10 ssh $1@$2
done
}
Run Code Online (Sandbox Code Playgroud)
我应该把它放在.bash_aliases或 的末尾.bashrc或 中.profile吗?
一方面,我不想把它放进去,.bash_aliases因为它会“破坏”列表
alias alias_name='command/list of commands'
Run Code Online (Sandbox Code Playgroud)
另一方面,.bashrc它看起来像是操作系统管理的文件,所以我不想向其中添加内容。
那么在 Ubuntu 中将功能添加为自定义命令的最佳实践是什么?