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

  • Home
  • SEARCH
  • 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 8266479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:10:37+00:00 2026-06-08T05:10:37+00:00

I’m developing custom grep wrapper for git. It shows first line of the file

  • 0

I’m developing “custom grep” wrapper for git. It shows first line of the file matching pattern. This line expected to be the header of the file.

I’ve created alias and everything went OK, until I tried search for pattern, which cannot be found in the working folder. Since no files were found, head and sed commands has nothing to work with and command hands and waits for input.

How to prevent this? I would like script to interrupt if no input is given. Definitely I could create separate script, assign list of files to variable and check length of this variable. But I would prefer sweet & neat oneliner-alias.

Is it possible? Here is my .gitconfig alias section:

[alias] 
    gg = !sh -c 'head -n1 -q `git grep --name-only $0` | sed s/OBJECT.//'

Resolution

Following commands for aliases worked for me on Windows 7 x64, msysGit 1.7.11.1:

gg = !sh -c 'head -n1 -q `git grep --name-only $0` < /dev/null | sed s/OBJECT.//' 

and

gg = !sh -c 'git grep --name-only $0 | xargs -l sed -n "1{s/OBJECT.//p}"'

But performance was different. The first approach is noticeably more fast.

  • 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-08T05:10:40+00:00Added an answer on June 8, 2026 at 5:10 am

    To address your immediate question on how to prevent head from stalling waiting for non-existent input, you can to use process substitution instead of command substitution:

    head -n1 -q <(git grep --name-only $0) | sed s/OBJECT.//
    

    Unlike command substitution where the “`cmd`` portion is replace by the output of the command, with process substitution the<(cmd)` portion is replace by a filename of a file/pipe that contains the output of the command.

    In the event where the command output is empty, head will still receive an input i.e. the filename of an empty file/pipe, and will proceed as you’d expect.

    P.S. note that you will need to use bash instead of sh in order to use process substitution.

    Update

    If your version of bash does not support process substitution, another way to get round the issue would be to redirect in contents from an empty or dummy file:

    head -n1 -q `git grep --name-only $0` < /dev/null | sed s/OBJECT.//
    

    Alternatively, you could simply pipe the output of git grep to xargs which allows you to then chain in calls to head or sed as shown in patthoyts answer.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.