Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-07-2003, 02:16 PM   #1
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
How do I pass a C variable to a Bash command ?

How do I pass a C variable to a Bash command ?

The program code below

*** system("du -ks /home/$user_name");

user_name is a C variable. I would like to have this variable
use in a bash command.
The program will run, but the above code will give
the hard drive space for /home/ not for /home/$user_name.


#include <stdio.h>

int main(void)
FILE *in;
char buffer[255];
char user_name[32];
int n;
int i = 0;
int j = 1;

if((in = fopen("/etc/samba/smbpasswd","r")) == NULL)
perror("Couldn't open file");
return 1;

/* fgets doesn't change the buffer if it doesn't read */
/* anything in. This means that the string from the */
/* last read is still there and the program will print */
/* the last line twice; therefore use the line below. */
buffer[0] = '\0';

fgets(buffer, 255, in);
n = strlen(buffer) - 1;
if (n != 0)
if(buffer[n] == '\n')
buffer[n] = '\0';
printf("buffer = %s\n", buffer);

for (i = 0; i <= 254; i++)
if (buffer[i] != ':')
user_name [i] = buffer[i];
user_name [i] = '\0';
printf ("j = %d\n", j);
printf ("user_name[] = %s\n", user_name);
printf ("\n");

*** system("du -ks /home/$user_name");

else printf("nothing read from file\n");

return 0;

system("du -ks /home/$user_name");
Old 07-07-2003, 02:23 PM   #2
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
system("du -ks /home/"user_name[]); maybe?

Last edited by Proud; 07-07-2003 at 02:24 PM.
Old 07-07-2003, 02:29 PM   #3
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Take a look at sprintf() (man sprintf).

What you will need to do is create a string variable to hold the command, and a string to hold the user's name. Something like this:
int main(void)
  char system_command[100];
  char user_name[20];

  // Get the user name
  scanf("Enter a user name: %s", user_name);

  // Now, put 'du -ks /home/' followed by the user's name
  // in the system_command variable.
  sprintf(system_command, "du -ks /home/%s", user_name);

  // Execute the command
EDIT: The scanf above should be split; it should be a printf followed by a scanf, but that's probably not that important. You already had the code to read the user's name anyway...

Last edited by Dark_Helmet; 07-07-2003 at 02:47 PM.
Old 07-07-2003, 02:31 PM   #4
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
Thumbs up Thank you all for helping

Dark_Helmet you got it. Thank you.


Space may be the final frontier, but it's made in a Hollywood basement.

Very True.

Last edited by Linh; 07-07-2003 at 02:46 PM.
Old 07-07-2003, 02:39 PM   #5
Senior Member
Registered: May 2003
Location: Sydney, Nova Scotia, Canada
Distribution: slackware
Posts: 4,185

Rep: Reputation: 60
now try what dark said
Old 07-07-2003, 02:40 PM   #6
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33

See dark_helmet's post for the correct method, with using sprintf(and if you're interested in hearing me rant about the negative aspects of using scanf, please ask!!). Also, please surround code posts with [code] [/code] tags, for the sake of readability.
Old 07-07-2003, 03:12 PM   #7
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Linh, sorry about the bad idea.

That line's from the title track of Californication by the Red Hot Chilli Peppers fyi.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass a variable as an agruement for cut chipmanchu Programming 1 05-25-2005 11:39 AM
How to pass mysql query to a variable? chynna_v Programming 4 09-03-2004 05:09 AM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM
Pass text to variable Zed Linux - Software 6 05-12-2003 03:02 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration