18.04 - 如何修复~/.profile和~/.bashrc语法错误

分享于 

4分钟阅读

ubuntu

  繁體

问题:

Ubuntu 18.04.4 (几周前从18.04.3自动升级)/bin/bash

在从Gnome shell登录后,桌面出现之前此消息会出现:


Error found when loading /home/hfinger/.profile: 



/home/hfinger/.profile: line 1: #: command not found 


/home/hfinger/.bashrc: line 1: syntax error near unexpected token '('


/home/hfinger/.bashrc: line 1: '# ~/.bashrc: executed by bash(1) for non-login shells.' 



As a result the session will not be configured correctly. 


You should fix the problem as soon as feasible. 




.profile, line 1: # ~/.profile: executed by the command interpreter for login shells. 


.bashrc, line 1: # ~/.bashrc: executed by bash(1) for non-login shells. 




答案1:


$ file profile bashrc


profile: UTF-8 Unicode (with BOM) text


bashrc: UTF-8 Unicode (with BOM) text



profilebashrc是my ~/.profile~/.bashrc的本地副本,字节序列为,


$ bash -c 'source profile; source bashrc'


profile: line 1: #: command not found


bashrc: line 1: syntax error near unexpected token `('


bashrc: line 1: `# ~/.bashrc: executed by bash(1) for non-login shells.'



最简单的解决方法是使用dos2unix,它默认删除BOM表:


$ dos2unix profile bashrc


dos2unix: converting file profile to Unix format...


dos2unix: converting file bashrc to Unix format...



$ file profile bashrc


profile: ASCII text


bashrc: UTF-8 Unicode text



检查非打印字符的其他方法是


cat -A ~/.profile ~/.bashrc



其中BOM将作为控制序列如M-oM-;M-?#或使用xxdod直接检查字节序列


head -1 ~/.profile | od -tx1



xxd -l16 ~/.profile



diff命令有助于确认存在差异,但不能确定差异是什么:


$ diff profile ~/.profile


1c1


< # ~/.profile: executed by the command interpreter for login shells.


---


> # ~/.profile: executed by the command interpreter for login shells.




答案2:

检查用户shell的简单方法。

如果没有安装Users and Groups应用程序,请以这种方式安装它。

sudo apt-get update

sudo apt-get install gnome-system-tools


回答3:

Ubuntu 18 . 04 . 4 (几周前从18 . 04 . 3自动升级)/bin/bash

我不知道问题是什么,但是这是我修复的方法:


$ cd ~ 


$ which bash


/bin/bash 


$ mv .profile .profile.bak 


$ mv .bashrc .bashrc.bak 


$ cd /etc/skel/ 


$ cp .profile ~/.profile 


$ cp .bashrc ~/.bashrc 



注意:/bin/bashbash的正确位置。



syn  FIX  BASH  Profile  errors  bashrc