2

I want to create an environment variable for this command:

git --git-dir= echo test-folder/.git/ add Steps-to-follow.txt && git --git-dir= echo test-folder/.git/ commit

This is a relevant excerpt from my ~/.bash_profile:

export git11='git --git-dir='
export equal==
export git12='test-folder/.git/ add Steps-to-follow.txt && git --git-dir='
export git13='test-folder/.git/ commit'

I tried $variable-name one after the other for all those variables but I think the shell blob, on expanding, is not passing it in the way I want it to appear (due to standards compliance of some type).

  1. I haven't really used export equal== but I have it there (I was trying random thing for it to work).

  2. I tried piping the commands but that's stupid, that is not supposed to work.

  3. I think the issue is with = sign in the environment variable (probably it is assuming the start of some other assignment). Meta character type input is not working (i.e., \= in place of =).

Some help in this will be really helpful.

techraf
  • 5,941
  • Do you know what shell functions are, and how to use them?  If so, why aren't you doing this with a shell function? P.S. The equal sign in the shell variable value is not a problem; the problem is the &&. – G-Man Says 'Reinstate Monica' Apr 03 '16 at 22:40
  • @jimmij point 1 in my post says it is not being used at all and I have not stressed on that at all apart from saying that it is there because I was trying some random things. I want to execute a command via environment variable(s) which I have said in the first line of my question quoting the command in question as well. I hope this helps a bit ? I wasn't my question was looking ambiguous – Avineshwar Apr 03 '16 at 23:06
  • @G-Man I am aware of some, however, I am more like if I see one, I can say what it is suppose to do. Can you shed some light on how to fix this?

    I figured out && is the bad guy here but if I am trying to combine two commands in one using && and then I want to use some environment variable for it, it is not possible at all I suppose ?

    – Avineshwar Apr 03 '16 at 23:08
  • Related: https://unix.stackexchange.com/questions/444946/how-can-we-run-a-command-stored-in-a-variable – Kusalananda May 23 '18 at 11:32

2 Answers2

2

I’m not familiar with git, so I don’t really understand what you are trying to do — in particular, why does your first command have the word echo twice, but then you didn’t put it into your variables?  But I guess that you want to do something like this:

Avineshwar_func() {
    git --git-dir="$1" echo test-folder/.git/ add Steps-to-follow.txt  &&  git --git-dir="$2" echo test-folder/.git/ commit
}

and then if you say

Avineshwar_func foo bar

it will be equivalent to saying

git --git-dir=foo echo test-folder/.git/ add Steps-to-follow.txt  &&  git --git-dir=bar echo test-folder/.git/ commit

Of course you can change the name from Avineshwar_func to whatever you want; e.g., git1.

Once you get this basically working, you should add error-handling; e.g.,

git1() {
    if [ -d "$1" ]  &&  [ -d "$2" ]
    then
        git --git-dir="$1" echo test-folder/.git/ add Steps-to-follow.txt  &&  git --git-dir="$2" echo test-folder/.git/ commit
    else
        echo "Usage: git1 dir1 dir2"
        return 1
    fi
}
0

Here's a working solution for it (RHEL 7 tested):

$ export test1='git --git-dir'
$ export test2='my-folder/.git/ add Steps-to-follow.txt'
$ #; git --git-dir'
$ export test3='my-folder/.git/ commit'
$ export git1=$test1=$test2
$ export git2=$test1=$test3
$ $git1
$ $git2

This will work. I cannot say this is the most optimal solution, however, it works the way I need and one might need it. && or ; (for firing multiple commands using one env. var.) didn't worked for me the way I expected it to work and hence I shifted to this acceptable solution for now. If someone comes up with something better or any improvement in this, please comment.