0

I wrote a simple script to switch my locale. If I write each line in the console and execute it, it works without any problem or if I put it in .bashrc.

However when I execute the script either with sudo or without it has absolutely no noticeable effect. ( locale remains the same )

The question is why is that?
Is my script wrong or am I missing something different.

Source:

#!/bin/bash set -x LANG=en_US.utf8 LANGUAGE=en_US.utf8 LC_ALL=en_US.utf8 export LANG export LANGUAGE export LC_ALL echo "Language set!" 

I'm receiving the execution steps and the Language set echo but that's about it. I also tried #!/bin/sh.


OS Info:

DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS" Kernel: 3.13.0-042stab103.6 

3 Answers 3

5

To apply the changes to your current shell you need to "source" it and not to "execute" your script. So, if your script is called "script.sh" then instead of executing it as ./script.sh, source it with . ./script.sh and your changes will be applied to the current session.

2
  • Works perfectly, I didn't know about the . Commented Sep 4, 2015 at 12:32
  • 1
    Just for completions sake, . is only a shorthand for source. Commented Sep 4, 2015 at 12:49
5

It seems normal. The change are only applied in the local shell session (ie the one which run the script), the parent shell are not affected.

If you run command in your current shell you are affecting your current shell, if you put it in your .bashrc then it also affects your current shell at startup.

But when you put it in a script a new shell is spawned to run the script as a child of your current script, so any modification of its environment will not affect its parents.

2
  • 1
    if you want to be able to switch locale for your current shell, you can choose to write a function in your .bashrc which you can call anytime in your current shell.This function will affect your current shell environment and doesn't require a new shell to be spawned for executing it. Commented Sep 4, 2015 at 12:15
  • 2
    You can apply those settings to the current shell using . ./change-language.sh Commented Sep 4, 2015 at 12:21
2

I had the same problem and finally solved it by using an alias in my ~/.bashrc

alias chlang="export LANG=en_US.utf8;export LANGUAGE=en_US.utf8;export LC_ALL=en_US.utf8" 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.