我知道 ubuntu 自带 gnome-terminal。几天前,我开始遇到一个无法描述的新问题。我在日常生活中使用 vscode 代码编辑器。许多命令,如 npm、pnpm、yarn、rust -v 等,在 gnome-terminal 中不起作用,我用相同的用户名用 .bashrc 文件配置了所有内容。我还使用另一个终端应用程序,如 hyper,所有命令都不起作用。它显示一个错误,如下所示
// pnpm
rajdeepsingh@officialrajdeepsingh:~$ pnpm -v
Command 'pnpm' not found, did you mean:
command 'npm' from deb npm (8.5.1~ds-1)
Try: sudo apt install <deb name>
rajdeepsingh@officialrajdeepsingh:~$
// deno
rajdeepsingh@officialrajdeepsingh:~$ deno -v
Command 'deno' not found, but can be installed with:
sudo snap install deno
rajdeepsingh@officialrajdeepsingh:~$
但是所有命令在 vscode 终端上都可以正常工作。我还没有找到解决方案。
我的 .bashrc 文件配置如下
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# 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
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
. "$HOME/.cargo/env"
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
# add bun config
BUN_INSTALL="/home/rajdeepsingh/.bun"
PATH="$BUN_INSTALL/bin:$PATH"
# add android studio config
export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:'/home/rajdeepsingh/Android/Sdk/platform-tools/'
# pnpm
export PNPM_HOME="/home/rajdeepsingh/.local/share/pnpm"
export PATH="$PNPM_HOME:$PATH"
# pnpm end
# Adding the deno config Manually add the directory to your $HOME/.bashrc (or similar)
export DENO_INSTALL="/home/rajdeepsingh/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
# Endding deno config
# surreadDB config start
export PATH=/home/rajdeepsingh/.surrealdb:$PATH
PATH=/home/rajdeepsingh/.surrealdb:$PATH
# surrealDB config end
# add bin path for meson
export PATH="/usr/local/bin:$PATH"
查找命令输出
rajdeepsingh@officialrajdeepsingh:~$ sudo find / -type f -iname 'pnpm'
[sudo] password for rajdeepsingh:
find: ‘/proc/15133/task/15133/net’: Invalid argument
find: ‘/proc/15133/net’: Invalid argument
find: ‘/proc/15146/task/15146/net’: Invalid argument
find: ‘/proc/15146/net’: Invalid argument
/tmp/tmp.IUj8SI8GhN/pnpm
/var/lib/docker/overlay2/02f58b453f8130db2dbb34489e2af94785d03602b43ef2ad0307e0d325c69669/diff/tmp/tmp.Ql1W7VIpZc/pnpm
/var/lib/docker/overlay2/02f58b453f8130db2dbb34489e2af94785d03602b43ef2ad0307e0d325c69669/diff/tmp/tmp.AStX5DdonL/pnpm
/var/lib/docker/overlay2/02f58b453f8130db2dbb34489e2af94785d03602b43ef2ad0307e0d325c69669/diff/root/.local/share/pnpm/pnpm
/var/lib/docker/overlay2/3f7b1a6a85187fd302f284d25a747fd2cacaee2b524223c962903e126638e157/diff/usr/local/lib/node_modules/corepack/shims/nodewin/pnpm
/var/lib/docker/overlay2/3f7b1a6a85187fd302f284d25a747fd2cacaee2b524223c962903e126638e157/diff/usr/local/lib/node_modules/corepack/shims/pnpm
/var/lib/docker/overlay2/6c8ce272d3f69240c3cb6f2b1b2fddc9c7e2b0e1dd891ac4be300d54137a5044/diff/tmp/tmp.WZQjSag9fK/pnpm
/var/lib/docker/overlay2/6c8ce272d3f69240c3cb6f2b1b2fddc9c7e2b0e1dd891ac4be300d54137a5044/diff/root/.local/share/pnpm/pnpm
/var/lib/flatpak/app/org.gnome.Builder/x86_64/stable/273caab574876b514a20e1ef86fe6e17d507f2d7e72ef3bbd02a3dc696aa8851/files/lib/node_modules/corepack/shims/nodewin/pnpm
/var/lib/flatpak/app/org.gnome.Builder/x86_64/stable/273caab574876b514a20e1ef86fe6e17d507f2d7e72ef3bbd02a3dc696aa8851/files/lib/node_modules/corepack/shims/pnpm
find: ‘/run/user/1000/doc’: Permission denied
find: ‘/run/user/1000/gvfs’: Permission denied
/home/rajdeepsingh/.local/share/pnpm/nodejs/16.17.0/lib/node_modules/corepack/shims/nodewin/pnpm
/home/rajdeepsingh/.local/share/pnpm/nodejs/16.17.0/lib/node_modules/corepack/shims/pnpm
/home/rajdeepsingh/.local/share/pnpm/nodejs/14.20.0/lib/node_modules/corepack/shims/nodewin/pnpm
/home/rajdeepsingh/.local/share/pnpm/nodejs/14.20.0/lib/node_modules/corepack/shims/pnpm
/home/rajdeepsingh/.local/share/pnpm/nodejs/16.17.1/lib/node_modules/corepack/shims/nodewin/pnpm
/home/rajdeepsingh/.local/share/pnpm/nodejs/16.17.1/lib/node_modules/corepack/shims/pnpm
/home/rajdeepsingh/.local/share/pnpm/global/5/.pnpm/@[email protected]/node_modules/@pnpm/exe/node_modules/.bin/pnpm
/home/rajdeepsingh/.local/share/pnpm/global/5/.pnpm/@[email protected]/node_modules/@pnpm/exe/pnpm
/home/rajdeepsingh/.local/share/pnpm/global/5/.pnpm/@[email protected]/node_modules/@pnpm/linux-x64/node_modules/.bin/pnpm
/home/rajdeepsingh/.local/share/pnpm/global/5/.pnpm/@[email protected]/node_modules/@pnpm/linux-x64/pnpm
/home/rajdeepsingh/.local/share/pnpm/global/5/.pnpm/node_modules/.bin/pnpm
/home/rajdeepsingh/.local/share/pnpm/pnpm
答案1
即使您设法解决了这个问题,我还是建议您尝试更好地了解这里到底发生了什么。首先,让我们回到您最初的错误消息:
Command 'pnpm' not found
发生这种情况的原因是您的 shell(可能是 bash)无法pnpm
在它需要查找的任何地方找到二进制文件。那么它需要查找哪里呢?您只需查看变量中的内容即可找到答案$PATH
:
echo $PATH
相对正常的输出可能看起来像这样:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/var/lib/snapd/snap/bin:/root/bin
这是用冒号分隔的“查找位置”列表,因此您可以ls
在终端中简单地写入,因为 bash 知道在其中一个文件夹中查找它并找到它。如果您想知道 bash 在哪里找到ls
,您可以执行以下操作:
# whereis ls
ls: /usr/bin/ls (...)
所以,如果我是你,我会尝试弄清楚:
- 当您输入时您实际上希望发生什么
pnpm
,该二进制文件位于哪里? - 确保该二进制文件所在的目录是 $PATH 变量的一部分
根据你问题中提供的输出,我假设你想要的pnpm
意思是/home/rajdeepsingh/.local/share/pnpm/pnpm
。首先,你可以通过运行二进制文件及其完整路径来测试这一点,例如:
$ /home/rajdeepsingh/.local/share/pnpm/pnpm
正如您在评论中提到的那样,位置是不是在您的 PATH 中,这解释了为什么 bash 不知道如何找到二进制文件:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
这条道路不仅看起来完全不同,而且没有什么来自您的.bashrc
。那么发生了什么?为什么您必须指定-i
交互模式?
出色地,你有这个在你的.bashrc
:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
.bashrc
我不知道你为什么会有这个,因为这不是我所知道的任何标准的一部分。一个基本的标准示例可能看起来像:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
因此,您必须运行的原因bash -i
不是因为“它就是这样工作的”,而是因为您自己(或其他人)添加了一个约束,.bashrc
如果您不运行,该约束就会退出bash -i
。
我的建议是:重新开始。创建一个新的.bashrc
,并按照您的需要进行设置,因为它并不复杂,但您却把它弄得很复杂。
你的.bashrc
可以可能看起来像这样:
# Source global definitions (system-wide)
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific environment (default)
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH
#( *** your config here, not related to PATH *** )
export PATH=$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH
export PATH=/home/rajdeepsingh/.bun/bin:$PATH
export PATH=$PATH:$HOME/Android/Sdk/tools
export PATH=$PATH:/home/rajdeepsingh/Android/Sdk/platform-tools/
export PATH=/home/rajdeepsingh/.local/share/pnpm:$PATH
export PATH=/home/rajdeepsingh/.deno/bin:$PATH
export PATH=/home/rajdeepsingh/.surrealdb:$PATH
# export PATH="/usr/local/bin:$PATH" <- pointless, /usr/local/bin is part of
# any normal path definition
尝试一下,source ~/.bashrc
然后echo $PATH
。