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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:21:08+00:00 2026-06-01T07:21:08+00:00

I have a theoretical question: 1) How pass a variable to the system of

  • 0

I have a theoretical question:

1) How pass a variable to the system of getline ()?

awk 'BEGIN{var="ls"; var | getline var; system("echo $var")}'

2) How to assign a variable the output system (“ls”) and print the result in awk?

awk 'BEGIN{var="system("ls")"; print '$var'}'

3) Can you assign a variable in the system (var = “ls”) and print the result in awk?

awk 'BEGIN{system(var="ls"); print "'"$var"'"}'

Thank you for the information.

EDIT:

torek: Thank you for your response.

I understand that in the first example, you can do this:

awk 'BEGIN { while ("ls -l" | getline var) system("echo " var  );}'

For this application, you can not assign a variable output from system ()? As in this example:

awk 'BEGIN {var="ls -l"; system(var); print var}'
  • 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-01T07:21:09+00:00Added an answer on June 1, 2026 at 7:21 am

    You’re looking at this the wrong way, I think. Awk’s system just takes any old string, so give it one, e.g.:

    system("echo " var);  # see side note below
    

    (remember that in awk, strings are concatenated by adjacency). Moreover, system just runs a command; to capture its output, you need to use getline, similar to your question #1.

    If you want to read all the output of ls you need to loop over the result from getline:

    awk 'BEGIN { while ("ls" | getline var) print "I got: " var; }'
    

    Since this defines only a BEGIN action, awk will start up, run ls, collect each output line and print it, and then exit.


    Side note: be very careful with variables passed to a shell (this includes both calls to system and items on the left hand side of | getline, plus some other cases in modern varieties of awk—anything that runs a command). Backquotes, $(command), and semicolons can all allow users to invoke arbitrary commands. For instance, in the system("echo " var) example above, if var contains ; rm -rf $HOME the command becomes echo ; rm -rf $HOME, which is almost certainly not something you want to have happen.

    You can check for “bad” characters and either object, or quote them. Modern 8-bit-clean shells should only require quoting quotes themselves (for syntactic validity), $, <, >, |, and `. If you use single quotes to quote arguments (to make them appear as a single “word”), you need only escape the single quotes. See this unix.stackexchange.com answer for more details.


    One other side note: I tend to add “unnecessary” semicolons to my awk scripts, making them look more like C syntactically. Old habit from decades ago.

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

Sidebar

Related Questions

I have a very theoretical question: Is there a way to ban the use
I have a rather theoretical question. My company has a working standard (documented) that
I have been struggling to find an answer to this theoretical question, even tho
This is more of a theoretical question than an actual problem I have. If
I have a theoretical question about the .net framework. As I see it Microsoft
More of a theoretical question, but wouldn't it make more sense to have only
Note:I don't mean some theoretical question which don't have any implementation just languages that
This is not a theoretical or general question about when to use pass by
I have theoretical question, is there a possibility to access server response or source
This is a theoretical question as I don't have an actual problem, but I

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.