1

I am new to Linux. I recently installed WSL2 (Ubuntu 22.04) on Windows 10. Whenever I open the terminal or type bash in the terminal, it keeps giving me an error. (Sorry, I was not able to copy from the terminal.) How do I resolve this?

enter image description here

Error in line 117 inside bashrc file

code inside bashrc starting line 117.

fi export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0 export
LIBGL_ALWAYS_INDIRECT=1 sudo /etc/init.d/dbus start &> /dev/null export DISPLAY=$(cat /etc/resolv.conf | grep
nameserver | awk '{print $2; exit;}'):0.0 export LIBGL_ALWAYS_INDIRECT=1 sudo /etc/init.d/dbus start &>
/dev/null
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
export LIBGL_ALWAYS_INDIRECT=1
sudo /etc/init.d/dbus start &> /dev/null
Vai_P
  • 21
  • 3
    It's pretty evident you have an error in line 117 of your ~/.bashrc - fix that. If you want help to fix the error, please post the relevant lines of your ~/.bashrc so people can have a look at it. – Artur Meinild Mar 04 '23 at 20:20
  • 1
    Welcome to Ask Ubuntu! See my answer on the duplicate question for how to enter Ubuntu on WSL without running the "problem file" (~/.bashrc). You can use that to revert any changes you made. If you aren't sure what the problem line is in the first place, then please use that method to get access to your ~/.bashrc and edit your question to include it. Then reply to this comment with @NotTheDr01ds in it, and I can reopen it. Thanks! – NotTheDr01ds Mar 04 '23 at 20:53
  • @NotTheDr01ds I edited the question and added an image of my bashrc file. thank you. – Vai_P Mar 05 '23 at 12:48
  • Did you happen to edit this file in a Windows editor? See this question and let me know if that helps. Otherwise, post the text of the file so that we can read it more easily to help you. Thanks! – NotTheDr01ds Mar 05 '23 at 13:08
  • 1
    Bring that line after fi starting with export ... down on a new line or add a semicolon after fi like so fi; export .... and use https://www.shellcheck.net/ to check your code ... There might be other syntax/structure errors in it. – Raffa Mar 05 '23 at 13:10
  • @NotTheDr01ds I am not sure that the duplicate post is going to help ... It appears to be bash specific random syntax error(s) IMHO. – Raffa Mar 05 '23 at 13:18
  • @Raffa Again, that other post would only help if it was a line-ending problem, but that doesn't appear to be the case here. – NotTheDr01ds Mar 05 '23 at 15:07
  • @Vai_P It seems you have at least 4 lines combined into one - I see three different export statements on that one line. – NotTheDr01ds Mar 05 '23 at 15:12
  • @NotTheDr01ds I didn't even notice the other exports are just laid down on the same line .... What an unlucky poor shell dealing with all those ... I guess suggesting to load bash without a profile at all might be rather more legitimate in such case after all :) ... It's hard to read from that screenshot anyway. – Raffa Mar 05 '23 at 15:47
  • Line 1: fi export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0 export ^-- SC1089 (error): Parsing stopped here. Is this keyword correctly matched up? ^-- SC2148 (error): Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive. @Raffa shellcheck.net gave me this output. – Vai_P Mar 07 '23 at 14:35
  • Your shell is clearly bash … So add #!/bin/bash Alone in the first line before the rest if the code … That is what’s called a shebang :-) – Raffa Mar 07 '23 at 14:45

2 Answers2

3

While @ArthurMeinild's answer is technically correct for fixing the errors, also note that those lines aren't needed on WSL any longer and should probably just be removed entirely.

As @Raffa mentioned in a comment, it appears you copied these from a website and pasted them in, losing the line-breaks on the first attempt.

You should determine why you are trying to use those lines in the first place and probably consider other (IMHO, better) options. Note that WSL has seen some massive changes and improvements over the last 18 months. If you are following instructions on a website/blog that pre-date that, they may be outdated already. And, of course, always be suspect of any website/blog post that doesn't include a posting or updated date.

As for these particular lines, what they do, and why they probably aren't needed:

  • The export DISPLAY line is only needed if you require a third-party X server, which most likely isn't the case any longer. WSL now includes the ability to run GUI applications directly, using WSLg rather than a third-party X server. Please see this answer for information on how to make sure you have updated to the latest release that supports this.

  • Even if you were using a third-party X server, there's a much better (IMHO) version of that DISPLAY line:

    export DISPLAY="$(hostname).local:0"
    

    See my Super User answer here along with my Stack Overflow explanation.

  • Likewise, LIBGL_ALWAYS_INDIRECT isn't needed with the new WSL implementation.

  • And WSL now can setup and run Systemd and D-Bus directly (if needed, which it often isn't) in the latest releases. See my Systemd answer here for information on how to set it up, and how to determine if you really need it.

  • Even if you were, for some reason, needing to start D-Bus directly at start-up, using sudo to do so in your user rc file isn't the best option. It means you have to type your password each time you start Ubuntu on WSL, which isn't the case normally.

    If you need to start a system service (apart from Systemd) when Ubuntu/WSL starts, create a /etc/wsl.conf file (as sudo) and use the [boot] section as in this answer.

As a result of this, I would highly recommend removing all of those additional lines and using the WSL features that accomplish the same thing directly.

NotTheDr01ds
  • 17,888
2

I think the lines you mention should be laid out like this:

fi 
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0 
export LIBGL_ALWAYS_INDIRECT=1 
sudo /etc/init.d/dbus start &> /dev/null 

Remove everything else that is redundant (remove everything after fi on line 117).

Artur Meinild
  • 26,018
  • 1
    Probably a heads-up note about the dangers of arbitrarily copying/pasting code from the internet into .bashrc especially when it has sudo would be extremely helpful … Plus using sudo in .bashrc and other files that are automatically repeatedly sourced should be generally avoided. – Raffa Mar 07 '23 at 14:52
  • Sure - busy atm, you're welcome to add. – Artur Meinild Mar 07 '23 at 15:00
  • @ArturMeinild hi! I followed your steps. It kind of worked but when I restarted the ubuntu terminal it is asking for a [sudo] password. After I enter the password it is taking me to the conda base environment. I do not know how or why conda got activated. Is this because of conda initialization steps written in .bashrc? I had to deactivate conda to come out of it. – Vai_P Mar 18 '23 at 09:48
  • @Raffa I did not copy-paste anything from the internet. Errors were there since the beginning (when I started using Linux a few weeks ago). Maybe I did something while installing. P.S I come from non-CS background slowly venturing into bioinformatics. I am slowly grasping the concepts and googling errors when I face them. When I was working on a pipeline I wasn't able to source .bashrc file. That is how it all started. – Vai_P Mar 18 '23 at 09:54
  • You have added something to your .bashrc that starts up the conda environment (which isn't part of your question btw). If you don't want this, why add it in the first place? – Artur Meinild Mar 18 '23 at 10:04
  • I will have to figure out why conda is being called when I open the terminal which has never happened before. And you are right this isn't part of the question. – Vai_P Mar 18 '23 at 10:27