Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to change a .config text file every time the program doesn't work as I want it to. I want to be able to do it in terminal preferably with an alias and with the following logic:

alias config='change line 17 to this THEN run this command THEN replace line 17 with what was at beginning'

So when I enter config into terminal the offending line will be changed, after which a command will be run, after which the line will be set to be what it was at beginning

share|improve this question
Sounds like an unclean way to handle an error. Wouldn't it be better to try to solve the problem? – Jacob Vlijm Apr 6 '14 at 15:56
I dont know how to solve disabling a dns when vpn is run and change back to normal dns when vpn exits:… – fdsfgdsdf Apr 6 '14 at 16:05
If I understand well, you need to be able to toggle between two config file versions? that should not be too difficult. It would maybe even be easyer (and saver) to have two versions of the file and make a script that would replace the real config file, depending on the contents of the config file. where is the file stored? – Jacob Vlijm Apr 6 '14 at 16:55
@Jacob the file is /etc/dhcp/dhclient.conf but I still dont know exactly how and what file will let me disable a dns from being used(please see Q above for detials) – fdsfgdsdf Apr 6 '14 at 16:59

This sounds very much like a band aid and an XY Problem and you would be better off figuring out the core problem. Still, these commands will do what you need. I have written it as a shell function, not an alias, but just add these lines to your ~/.bashrc file:

    ## Change current working directory to the directory where .config file is located
    cd /path/to/config_file
    ## The original 17th line
    old="$(sed '17q;d' .config)"
    ## The one you will replace it with
    new="new line"
    ## The sed command that does the replacing
    sed -i.bak "17 s/$old/$new/" .config
    ## Run whatever needs to be run with the modified file
    ## Copy the file back
    cp .config.bak .config
    ## Go back to the old working directory
    cd $OLDPWD

You can then run it by running fix_config.

share|improve this answer
Your solution seems wonderful and I cant wait to try it but Im having issue with the config setting being ignored (details here… ) – fdsfgdsdf Apr 6 '14 at 23:07

Here is a verbose solution as a python script. Not literally what you were asking for, but yet a solution:


import os

# change the lines below to the correct paths
path_to_configfile = "path_to_configfile" # the real one in  /etc/dhcp/dhclient.conf
path_toconfigfile_a = "path_toconfigfile_a"
path_toconfigfile_b = "path_toconfigfile_b"
# change the line to the default line in the config file (leave the \n)
old_line = "old_line\n"
# change to the line number to check as an indicator
line_number = 17 # first line =  1
# change to the messages you'd like to see
config1_message = "config 1 is activated"
config2_message = "config 2 is activated"

with open(path_to_configfile) as defaultconfig:
    defaultconfig = defaultconfig.readlines()

if defaultconfig[line_number-1] == old_line:
    os.system('gksudo cp '+"'"+path_toconfigfile_a+"'"+" "+"'"+path_to_configfile+"'")
    os.system('zenity --info --text '+'"'+config1_message+'"')
    os.system('gksudo cp '+"'"+path_toconfigfile_b+"'"+" "+"'"+path_to_configfile+"'")
    os.system('zenity --info --text '+'"'+config2_message+'"')

To use it:

  • create a folder in a secure directory to store the two versions of your config files.
  • create a ~/bin folder.
  • copy the code above into a file and call it toggle_config (or anything you like).
  • change the lines in the headsection to the correct paths, line and messages you'd like to see when the settings are toggled
  • save the file and make it executable. store it in ~/bin, or copy it to /usr/bin for more security.

After logout / login you will be able to toggle between the two setting versions, by the command


You will be asked for your password, and you will see a message like this:

enter image description here

Alternatively, you can create a .desktop file with the command and lock it to the launcher for quick switching between the settings.

share|improve this answer
Your solution seems so awesome and I cant wait to try it but Im having issue with the config setting being ignored (details here… ) – fdsfgdsdf Apr 6 '14 at 23:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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