Bash variables in command

6,882

Solution 1

#!/bin/bash

display_x=640
display_y=480

xrandr -s ${display_x}x${display_y}

Solution 2

You should always put shell variables into quotes unless you have a good reason not to, and you’re sure you know what you’re doing.  So Deathgrip’s answer should be

xrandr -s "${display_x}x${display_y}"

and that is the way I would probably do it.  But

xrandr -s "$display_x"x"$display_y"

will also work.  Here’s another approach:

display_x=640
display_y=480
x=x
xrandr -s "$display_x$x$display_y"

— anything to tell the shell that you’re not trying to reference a variable named display_xx.

Share:
6,882

Related videos on Youtube

xinthose
Author by

xinthose

Updated on September 18, 2022

Comments

  • xinthose
    xinthose over 1 year

    I would like to make this command xrandr -s 640x480 use variables like so

    #!/bin/bash
    
    display_x=640
    display_y=480
    
    xrandr -s $display_xx$display_y
    

    The command does not run correctly. How can I do this?

  • xinthose
    xinthose almost 7 years
    Oh, braces, I saw that somewhere online. Thank you sir.