The command-substitution tag has no wiki summary.
5
votes
2answers
142 views
Special character '#' in Perl SSH command
I try to send command using SSH in my perl script with # but it gets truncated right at #
Example:
Input text is :
$message = "Product ID # STK000134"
The SSH command is :
$execute=`ssh -q ...
2
votes
5answers
87 views
Command substitution - single quotes and spaces
How can I make command substitution pass a list of single-quoted POSIX paths separated by spaces?
command1 $(command2)
command1 '/path/to/file 1' '/path/to/file 2' '/path/to/file 3'
How can I trace ...
2
votes
1answer
81 views
Compare two data streams without both being stored as files
I have these two files:-
[root@localhost base_filters]# cat aix_old
joe
amadeus
image
bill
juliet
charlie
romeo
ftp
[root@localhost base_filters]# cut -d: -f1 passwd2
henry
amadeus
image
bill
julie
...
0
votes
1answer
48 views
Command substitution in alias resolved in bash profile? [duplicate]
I want to make an alias for randomly changing my mac address
alias chrandmac="sudo ifconfig en0 ether $(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')"
but the command substitution part is ...
1
vote
1answer
65 views
starting vim with command substitution
I am working on a project on two different machines - one running Mac OSX 10.8.3, and one running Red Hat Enterprise Linux. On my Mac, I can do this:
vim $(ls -R */*.@(h|cpp) */*/*.@(h|cpp))
and ...
3
votes
1answer
65 views
Different behavior of $() and `` [duplicate]
% PATH="MYPATH"
% VAR="PATH"
% echo $(eval echo \$$VAR)
MYPATH
% echo `eval echo \$$VAR`
5707VAR
^^
This is the process number.
I thought those two were exactly the same, but obviously there are ...
1
vote
2answers
55 views
Configure bash to execute command with last argument when no argument was provided
I would like to configure bash so that when I execute command (preferably from a list of commands, not any command) without an argument, it takes the argument of previous command.
So, for example I ...
2
votes
2answers
77 views
How to pass the output of a script to a command like ls without the output being split?
Suppose I have a script like this:
#!/bin/bash
printf '%q\n' "b c"
Executing the script prints:
b\ c
on the commandline.
Now, being in a directory which contains a file named b c I want to pass ...
2
votes
1answer
172 views
How does wc -l `ls` give the actual number of lines?
Can anyone explain to me how does this command work.
wc -l `ls`
while this command gives the total number of java lines or txt lines.
1
vote
5answers
184 views
Run a command on all the files in a directory tree and put the output into a variable
I want to run this bash command :
#!/bin/bash
rep="*"
for f in `ls -R`$rep; do
d='git log '$f'| wc -l'
c=$d
echo $c
done
how to excute a command git log myFile | wc -l from bash ?
ps : this ...
7
votes
3answers
860 views
Understanding backtick (`)
I am trying out the command
$ b=5; echo `$b`;
-bash: 5: command not found
but it does not print 5 as it is supposed to. What am I missing here?
What does ` (backquote/backtick) mean in bash? seems ...
3
votes
3answers
522 views
Split command output by linebreak?
I have a command returning multiple lines. For further processing I need to process each single line of those lines.
My current code works by modifying the IFS (Internal Field Separator):
...
2
votes
1answer
179 views
Command substitution: file or command not found
The following script
#!/bin/bash
QUERY='select * from cdr;'
MYROWS=$("sqlite3 -list -nullvalue NULL -separator ',' /var/log/asterisk/master.db '${QUERY}'")
gives me
./bla.sh: row 35: sqlite3 -list ...
7
votes
3answers
717 views
Multivariable For Loops
Is there a way to specify multiple variables (not just integers) in for loops in bash? I may have 2 files containing arbitrary text that i would need to work with.
What i functionally need is ...
3
votes
1answer
142 views
Command substitution: cat with executable content
I have a file called test and the contents are:
ubuntu@regina:~$ cat test
** test **
catting this file via command line works fine, but if I use command substitution I get an understandable but ...
7
votes
4answers
1k views
Command substitution: splitting on newline but not space
I know I can solve this problem several ways, but I'm wondering if there is a way to do it using only bash built-ins, and if not, what is the most efficient way to do it.
I have a file with contents ...
1
vote
2answers
298 views
Create MySQL database name using variable from date
for the variable
dbnya="echo $(date +%Y%m%d%H%M%S)"
When running below code, I will get an error (SQL syntax error)
mysql -u root -pthepass -e "CREATE DATABASE demo$dbnya CHARACTER SET utf8 ...
7
votes
1answer
903 views
How can I execute `date` inside of a cron tab job?
I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
...
3
votes
1answer
1k views
Bash: Merge foldername from variable with filename
First I write a configfile with all my parameters like this
path="/home/test/"
I name it test.conf.
Then I write a shell script with this content, name it test, and make it executable with chmod ...
6
votes
4answers
228 views
Run a Command and then Parameter Substitution in one line
bash# hostname
host1.example.com
I only want host1. So:
SHORT_HOST=$(/bin/hostname)
SHORT_HOST=${SHORT_HOST%%.*}
Can I turn this into a one liner?
- or -
what is the best way to make $SHORT_HOST ...
6
votes
5answers
896 views
Why does shell Command Substitution gobble up a trailing newline char?
As per the following example, and as in my recent question In bash, where has the trailing newline char gone?, I want to know "why" it happens
x="$(echo -ne "a\nb\n")" ; echo -n "$x" | xxd -p
# ...
10
votes
2answers
463 views
Where has the trailing newline char gone from my command substitution?
The following code best describes the situation. Why is the last line not outputting the trailing newline char? Each line's output is shown in the comment. I'm using GNU bash, version 4.1.5
...
4
votes
4answers
736 views
file $(ls /usr/bin/* | grep zip) command gives me errors. What's wrong?
I'm a total noob when it comes to unix/linux commands and I decided to read a book.
I've reached a chapter where they try to explain how to pass the output of commands as expansions to the shell.
...
96
votes
3answers
5k views
What's the difference between $(stuff) and `stuff`?
Running top -p $(pidof init) and top -p `pidof init` gives the same output. Are these two ways of doing the same thing, or are there differences?