How to use an environment variable inside a quoted string in Bash

前端 未结 5 1195
温柔的废话
温柔的废话 2020-12-13 03:17

I\'ve tried various forms of the following in a bash script:

#!/bin/bash
svn diff $@ --diff-cmd /usr/bin/diff -x \"-y -w -p -W $COLUMNS\"

B

5条回答
  •  臣服心动
    2020-12-13 03:57

    Note that COLUMNS is:

    1. NOT an environment variable. It is an ordinary bash parameter that is set by bash itself.
    2. Set automatically upon receipt of a SIGWINCH signal.

    That second point usually means that your COLUMNS variable will only be set in your interactive shell, not in a bash script.

    If your script's stdin is connected to your terminal you can manually look up the width of your terminal by asking your terminal:

    tput cols
    

    And to use this in your SVN command:

    svn diff "$@" --diff-cmd /usr/bin/diff -x "-y -w -p -W $(tput cols)"
    

    (Note: you should quote "$@" and stay away from eval ;-))

提交回复
热议问题