Score:1

How to fix BASH errors in line 117 in .bashrc file?

tm flag

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
Artur Meinild avatar
vn flag
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.
NotTheDr01ds avatar
vn flag
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](https://askubuntu.com/q/1457778/1165986) your question to include it. Then reply to this comment with @NotTheDr01ds in it, and I can reopen it. Thanks!
Vai_P avatar
tm flag
@NotTheDr01ds I edited the question and added an image of my bashrc file. thank you.
NotTheDr01ds avatar
vn flag
Did you happen to edit this file in a Windows editor? See [this question](https://askubuntu.com/q/803162/1165986) 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!
Raffa avatar
jp flag
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 avatar
jp flag
@NotTheDr01ds I am not sure that the duplicate post is going to help ... It appears to be bash specific random syntax error(s) IMHO.
NotTheDr01ds avatar
vn flag
@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 avatar
vn flag
@Vai_P It seems you have at least 4 lines combined into one - I see three different `export` statements on that one line.
Raffa avatar
jp flag
@NotTheDr01ds I didn't even notice the other `export`s 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.
Vai_P avatar
tm flag
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.
Raffa avatar
jp flag
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 :-)
Score:3
vn flag

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.

Score:2
vn flag

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).

Raffa avatar
jp flag
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.
Artur Meinild avatar
vn flag
Sure - busy atm, you're welcome to add.
Vai_P avatar
tm flag
@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 avatar
tm flag
@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.
Artur Meinild avatar
vn flag
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?
Vai_P avatar
tm flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.