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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:12:43+00:00 2026-06-02T03:12:43+00:00

Is it possible to define a macro-function in bash so when I write: F(sth);

  • 0

Is it possible to define a macro-function in bash so when I write:

F(sth);

bash runs this:

echo "sth" > a.txt;
  • 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-02T03:12:44+00:00Added an answer on June 2, 2026 at 3:12 am

    Arbitrary syntax can’t be made to do anything. Parentheses are metacharacters which have special meaning to the parser, so there’s no way you can use them as valid names. The best way to extend the shell is to define functions.

    This would be a basic echo wrapper that always writes to the same file:

    f() {
        echo "$@"
    } >a.txt
    

    This does about the same but additionally handles stdin – sacrificing echo's -e and -n options:

    f() {
        [[ ${1+_} || ! -t 0 ]] && printf '%s\n' "${*-$(</dev/fd/0)}"
    } >a.txt
    

    Which can be called as

    f arg1 arg2...
    

    or

    f <file
    

    Functions are passed arguments in the same way as any other commands.

    The second echo-like wrapper first tests for either a set first argument, or stdin coming from a non-tty, and conditionally calls printf using either the positional parameters if set, or stdin. The test expression avoids the case of both zero arguments and no redirection from a file, in which case Bash would try expanding the output of the terminal, hanging the shell.

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

Sidebar

Related Questions

With gcc (and possibly other compilers as well) it's possible to define a macro
Possible Duplicates: What’s the use of do while(0) when we define a macro? Why
Is it possible to define more then one function in AJAX Enabled WCF Service
I want to create a macro (or function) which will contain package-prefixed symbols. This
How do you write a macro with variable number of arguments to define a
Possible Duplicate: How does this “size of array” template function work? Is there any
Is it possible to define a C/C++ macro BUILD(a, i) which expands to x[0],
Is it possible to write a C macro that returns the number of its
Possible Duplicate: "static const" vs "#define" in c A macro is a fragment of
Possible Duplicate: how does do{} while(0) work in macro? Example from this blog post:

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.