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
up vote 5 down vote accepted

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.

  • Works perfectly, I didn't know about the . – deW1 Sep 4 '15 at 12:32
  • 1
    Just for completions sake, . is only a shorthand for source. – Jakob Lenfers Sep 4 '15 at 12:49

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.

  • 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. – netmonk Sep 4 '15 at 12:15
  • 2
    You can apply those settings to the current shell using . ./change-language.sh – Lambert Sep 4 '15 at 12:21

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"

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.