I have a git repository with 2 branches:  $ git branch * master   test/branch   I can list specific branches individually by doing the following:  $ git branch --list master * master  $ git branch --list test/branch   test/branch   However, when I store this command as a variable, I get unexpected results:  $ LOCAL=$(git branch --list master); echo $LOCAL index.php readme.md master  $ LOCAL=$(git branch --list test/branch); echo $LOCAL test/branch   The results aren't always consistent. Sometimes I get unexpected results from branches with forward slashes, sometimes without, depending on the repository I'm working with. I can't put my finger on what's happening exactly or why.  Why does listing one branch list files in the directory and the branch itself, and the other one just lists the branch?
1847
Or, an introductory guide to robust filename handling and other string passing in shell scripts.  I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names).  I encountered a problem such as the following:   I have a file name containing a space hello world, and it was treated as two separate files hello and world. I have an input line with two consecutive spaces and they shrank to one in the input. Leading and trailing whitespace disappears from input lines. Sometimes, when the input contains one of the characters \[*?, they are replaced by some text which is is actually the name of files. There is an apostrophe ' (or a double quote ") in the input and things got weird after that point. There is a backslash in the input (or: I am using Cygwin and some of my file names have Windows-style \ separators).   What is going on and how do I fix it?
76806
Below is some sort of pseudo-code for what I'm trying to accomplish:  #!/bin/bash  # I already have the variable below figured out (positive integer): numlines=$([returns number of lines containing specific characters in a file])  # This is basically what I want to do with it: for i in {1..$numlines}; do     # the part below is already figured out as well:             do some other stuff done   I can execute it fine from the command line by inserting the actual number in the `{1..n}' sequence.  I just need to know if it's possible to include a variable here and how to go about doing it.   I have tried exporting it I have tried putting the variable itself in curly braces inside the sequence: {1..${numlines}} I have tried putting it in double-quotes hoping it would expand: {1.."$numlines"} I have tried escaping the $: {1..\$numlines}   Do I need to use a set -[something] command in order for this variable to get expanded?  I have even tried some forms of using eval...all to no avail.  I just need to know if there is something simple or obscure that I am missing or if this is even possible before I waste anymore time on it.  I could throw together a really, really hackish way of doing it to make it work as needed, but I'd like to avoid that if at all possible and learn the right way to go about doing it.
8716
I am looking for a simple Git command that provides a nicely formatted list of all files that were part of the commit given by a hash (SHA-1), with no extraneous information. I have tried: git show a303aa90779efdd2f6b9d90693e2cbbbe4613c1d  Although it lists the files, it also includes unwanted diff information for each. Is there another git command that will provide just the list I want, so that I can avoid parsing it from the git show output?
101845
I have an unusual idea to use git as a backup system. So let's say I have a directory ./backup/myfiles and I want to back that up using git. To keep things clean I don't want to have a .git directory in the myfiles folder, so I thought I could create ./backup/git_repos/myfiles. From looking at the git docs, I've tried doing this:  $ cd backup/myfiles $ mkdir ../git_repos/myfiles $ git --git-dir=../git_repos/myfiles init Initialized empty Git repository in backup/git_repos/myfiles/ $ git --git-dir="../git_repos/myfiles/" add foo fatal: pathspec 'foo' did not match any files   You can see the error message I get there. What am I doing wrong?
21443
Is there any standard that covers the portability of running a command after variable assignment on the same line?  APPLE="cider" echo hi   How portable is something like that? Where will it work and where won't it?   Also: my shell scripts start with #!/bin/sh if that makes any difference.
10665
This question is quite similar to , but I can't understand the solution. I also know , but still I am unable to understand what I am doing wrong.  I am trying to autocomplete the list of directories that are placed in /something/:  $ ls /something/  One    Other    Three   in a way that this will happen:  $ hi [TAB]  One Other Three $ hi O[TAB]  One Other   Only when the first word is hi and not only inside /something/.  This is what I am doing in .profile:  _codeComplete() {     local cur=${COMP_WORDS[COMP_CWORD]}     COMPREPLY=( $(compgen `ls /something/` -- $cur) )  } complete -F _codeComplete "hi "
45221
&gt; echo "hi" hi &gt; VAR='echo "hi"' &gt; $VAR "hi"   Why is the output of the above commands different?  A similar thing occurs with single quotes:  &gt; VAR="echo 'hi'" &gt; $VAR &gt; 'hi'
1260
I am trying to print a simple value for $AR1_p1 but the variable $i is not evaluating. for i in 1 2 3 4 do     AR1_p1=22     AR1_p2=23     AR1_p3=24     AR1_p3=25     echo $AR1_p$i done   It's like concatenating dynamically. Any suggestions on how to fix this?.
81209
I'm trying to put together a generic script that will check the existence of several key directories for various users on different servers. Additionally, I want to leverage each user's $HOME variable.  For eg, let's say this were true:   on server 1: jdoe's home is /home/jdoe  on server 2: jdoe's home is /opt/jdoe2 server 3 hasn't been built yet; we won't know where they build his $HOME until the server is built. on server 4: mysql's home is /opt/home/mysql   This is what I have for my important directories (ordered from most to least impt):  $ cat mylist.txt $HOME/most_impt_dir1 $HOME/most_impt_dir2 $HOME/most_impt_dir3 $HOME/misc $HOME/junk   ...I want to find the most impt dir owned by this user.   Here's what I'm trying:  for i in `cat mylist.txt` do    if [[ -O $i ]] &amp;&amp; [[ -d $i ]]; then     echo "found it: $i"     break   else     echo "$i is not it."   fi  done   The above code does not work for anything in my list because it is literally checking for dirs beginning with $HOME. How do I get my code to use the value of the user's $HOME variable?
100903
Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo.  Is this the convention or is it only for global variables?  What about variables local to the script?
41257
Run these commands:  [$] echo 'export VAR="qwertyuiop" export VAR="$VAR:asdf=ghjkl"' &gt; /tmp/vars.sh [$] source /tmp/vars.sh [$] echo $VAR /home/saga/qwertyuiopsdf=ghjkl   As you can see, the variable substitution does not do what we expected. What is the issue here?
89995
If I have a PATH variable that (when exploded onto multiple lines) contains something like this (with a few notes added by me):  /Users/brandon/.rvm/gems/ruby-2.0.0-p353/bin /Users/brandon/.rvm/gems/ruby-2.0.0-p353@global/bin /Users/brandon/.rvm/rubies/ruby-2.0.0-p353/bin /opt/local/bin /opt/local/sbin /usr/local/sbin /usr/local/bin   # &lt;&lt;--- notice this path appears again later /usr/bin         # &lt;&lt;--- notice this comes AFTER /usr/local/bin /bin /usr/sbin /sbin /usr/local/bin   # &lt;&lt;--- here it is again! /usr/local/git/bin /Users/brandon/bin /Users/brandon/.homebrew/bin /usr/local/mysql/bin /usr/local/share/npm/bin /Applications/Postgres93.app/Contents/MacOS/bin /Users/brandon/.rvm/bin   is it possible for the presence of the second /usr/local/bin/ to cause the /usr/bin to take precedence over /usr/local/bin?  This seems crazy (to me: I've never heard of such a thing) but that's exactly the behavior I'm seeing.  I've just installed PHP with Homebrew, and yet the system PHP (from /usr/bin) is taking precedence:  $ which php /usr/bin/php   BUT, after manually altering the PATH (rather than digging through all the files sourced by my .zshrc to try to figure out why &amp; where the duplication is coming from) to remove the second instance of /usr/local/bin, I find that /usr/local/bin is taking precedence over /usr/bin as I would have expected it to in the first place:  $ which php /usr/local/bin/php   What would cause this?  Is this normal?
55808
I have my Git repository which, at the root, has two sub directories:  /finisht /static   When this was in , /finisht was checked out in one place, while /static was checked out elsewhere, like so:  svn co svn+ssh://admin@domain.com/home/admin/repos/finisht/static static   Is there a way to do this with Git?
123071
I have the following in a script  for server in ${servers[@]}; do     echo ${server}     ssh user@${server} "for i in /tmp/foo* ; do echo ${i}; done" done   But it doesn't work. Weird thing, I see $I amount of return lines. So if I have ten files, I see ten blank lines.
2425
I need to exclude .git from my find search. To achive that, I am using the -path ./.git -prune switch:  $ find . -path ./.git -prune -o \( -type f -o -type l -o -type d \) | grep '.git' ./.git   However, even though this skips the contents of the .git directory, it lists the directory itself. It works when I add -path ./.git -prune -o -print -a  find . -path ./.git -prune -o -print -a \( -type f -o -type l -o -type d \) | grep '.git'   Why is this necessary. I thought that both commands should have same output. The second syntax is quite ugly.
35624
As I understand it when Git assigns a SHA1 hash to a file this SHA1 is unique to the file based on its contents.   As a result if a file moves from one repository to another the SHA1 for the file remains the same as its contents have not changed.   How does Git calculate the SHA1 digest? Does it do it on the full uncompressed file contents?   I would like to emulate assigning SHA1's outside of Git.
99178
Consider I've set variable site and needs to be printed by echo or printf, but If I use single quote to write something and want to use variable then how?  Example:  $ site=unix.stackexchange.com $ echo "visit:$site" visit:unix.stackexchange.com   But If I use single quote:  $ echo 'visit:$site' visit:$site   Then we know that '' is strong quote and will not expand the variable   I've tried something:  $ echo 'visit:"$site"' visit:"$site"   but do not succeed. So, I am looking for way to print value inside variable while using single quote.