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 8958661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:15:56+00:00 2026-06-15T15:15:56+00:00

This command prints: $ echo 123456789 | awk ‘{ print substr ($1,1,4) }’ 1234

  • 0

This command prints:

$ echo "123456789" | awk '{ print substr ($1,1,4) }'
1234

Is it possible to execute a string as command? For example, this command:

echo "123456789" | awk '{a="substr"; print a ($1,1,4) }'

Result:

$ echo "123456789" | awk '{a="substr"; print a ($1,1,4) }'
awk: {a="substr"; print a ($1,1,4) }
awk:                               ^ syntax error

EDIT:

$ cat tst.awk 
function my_substr(x,y,z) { return substr(x,y,z) }

{ a="my_substr"; print @a($1,1,4) }
bolek@bolek-desktop:~/Pulpit$ echo "123456789" | gawk -f tst.awk
gawk: tst.awk:3: { a="my_substr"; print @a($1,1,4) }
gawk: tst.awk:3:                        ^ nieprawidłowy znak '@' w wyrażeniu
bolek@bolek-desktop:~/Pulpit$ 
  • 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-15T15:15:57+00:00Added an answer on June 15, 2026 at 3:15 pm

    You can call user-defined functions via variables in GNU awk using indirect function calls, see http://www.gnu.org/software/gawk/manual/gawk.html#Indirect-Calls

    $ cat tst.awk
    function foo() { print "foo() called" }
    function bar() { print "bar() called" }
    
    BEGIN {
       the_func = "foo"
       @the_func()
    
       the_func = "bar"
       @the_func()
    }
    $ gawk -f tst.awk
    foo() called
    bar() called
    

    Unfortunately due to internal implementation issues, if you want to call builtin functions that way then you need to write a wrapper for each:

    $ cat tst.awk
    function my_substr(x,y,z) { return substr(x,y,z) }
    
    { a="my_substr"; print @a($1,1,4) }
    $ echo "123456789" | gawk -f tst.awk
    1234
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute a command like this: find ./ -name *.gz -print
I'm using GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu). And this command: echo -e doesn't print
I found this command line search and replace example: find . -type f -print0
I'm trying to make this command: sed bla bla filename | awk '{printf %s
How do I run this command with subprocess? I tried: proc = subprocess.Popen( '''ECHO
The following code works fine #!/bin/bash while true; do echo $(pwd | awk '{print
My example string is as follows: This is 02G05 a test string 20-Jul-2012 Now
I have code: sudo_cmd=echo -e \\$cmd\\ >> $CACHE_CONF echo $sudo_cmd that prints string: echo
I see that this is the same question as Making cmake print commands before
This command and output: % find . -name file.xml 2> /dev/null ./a/d/file.xml % So

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.