Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:11:51+00:00 2026-06-17T18:11:51+00:00

Im writing a bash script to help me set up my rpi’s network interfaces

  • 0

Im writing a bash script to help me set up my rpi’s network interfaces faster. It is mostly finished now and works using sed to alter different parts of the /etc/network/interfaces files depending how the network interface is being set up. My problem is that i am inserting quotation marks where i don’t want them and if i remove the quotation marks i insert the variables containing the data to be inserted as a string of the variable name as oppose to inserting them as their value.

My code is as followed (although i have cut it down)

#!/bin/bash
ANS=''
ssid=''
psk=''
file='/etc/network/interfaces'
ip=''
netmask=''
broadcast=''
gateway=''
function static {
    echo 'Will now set up Static IP'
    echo 'What IP address do you want to assign this Device?'
    echo -e '> \c'
    read ip
    echo 'What is your Networks Netmask/Subnet?'
    echo -e '> \c'
    read netmask
        echo 'What is your Networks Broadcast Address?'
        echo -e '> \c'
        read broadcast
        echo 'What is the address of your Networks Gateway?'
        echo -e '> \c'
        read gateway
    echo 'You entered the following information about your Network'
    echo ''
    echo 'Address: ' $ip
    echo 'Netmask: ' $netmask
    echo 'Broadcast: ' $broadcast
    echo 'Gateway: ' $gateway
    echo 'Is this information correct? (y/n)'
    echo -e '> \c'
        read ANS
        if [ $ANS = 'y' ]; then
        sed -i "s/^iface eth0 inet dhcp.*\$/iface eth0 inet static/" $file
        sed -i "s/^#    address.*\$/    address \"$ip\"/" $file
        sed -i "s/^#    netmask.*\$/    netmask \"$netmask\"/" $file
        sed -i "s/^#    broadcast.*\$/  broadcast \"$broadcast\"/" $file
        sed -i "s/^#    gateway.*\$/    gateway \"$gateway\"/" $file
        echo 'Static IP address has now been set up'
        start
    else
        static
    fi
}

The problem is these lines here

        sed -i "s/^#    address.*\$/    address \"$ip\"/" $file
        sed -i "s/^#    netmask.*\$/    netmask \"$netmask\"/" $file
        sed -i "s/^#    broadcast.*\$/  broadcast \"$broadcast\"/" $file
        sed -i "s/^#    gateway.*\$/    gateway \"$gateway\"/" $file

which insert address "(var_address)". As i said i can get it to insert address $address. But not, as i want, address (var_address). Could someone please explain how to use the / and \ together with ” and ‘ in the sed command so that i can fix the problem. As you may have guessed im a bit of a newb to bash so any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:11:52+00:00Added an answer on June 17, 2026 at 6:11 pm

    While bash won’t try to resolv no standard variables, like $/, you could simply:

    sed "
        /^iface $iface/,/^$/{
            /address/s/^.*$/\taddress $ip/;
            /netmask/s/^.*$/\tnetmask $netmask/;
            /broadcast/s/^.*$/\tbroadcast $broadcast/;
            /gateway/s/^.*$/\tgateway $gateway/;
    
        }" -i $file
    

    Nota: This modify only 1 paragraph concerning $iface: with the first sed line: /^iface $iface/,/^$/{ ... } delimit a command block who could be executed only from a line matching /^iface $iface/ to an empty line (or end of file).

    And for playing with IP address in bash, take a look at https://serverfault.com/a/461831/142978

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Bash script and using Expect to do sftp. Now in
While writing a bash script to help creating polaroid thumbnail using Imagick's convert commmand.
Im writing a bash-script to perform an offsite backup, using rsync over SSH. I'm
I'm writing a bash script and I have errexit set, so that the script
I am writing a Bash script to set some environment variables and then start
Thanks for your help in advance. I'm writing a simple bash script, which just
I am currently writing a bash script and I'm using curl. What I want
I am writing a bash script which accept parameters. I am using getopts to
I am writing a bash script to set up a production server. The tasks
I'm writing a bash script in which I read single characters from the input.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.