All Questions
Tagged with variable command-line
20
questions
6
votes
2
answers
409
views
How do I specify chance when setting a variable to random item in array?
Currently I do something like this to set a variable to a random item in an array:
array=("foo" "bar" "baz")
var=${array[$RANDOM % ${#array[@]} ]}
How would I do if I want to set $var to one of ...
4
votes
1
answer
3k
views
Get previous week's number in bash
I need the number of the previous week of the month.
In the 2nd week of March, the previous week would be 1. In the 1st week of April, the previous week would be 5.
My week starts with Monday.
...
3
votes
2
answers
2k
views
How can I expand all variables at the command line in Zsh?
In an earlier question, specific to bash, I asked How can I expand a relative path at the command line, with tab completion?
I liked @Mikel's answer that mentioned using Ctrl+Alt+e to expand all ...
3
votes
2
answers
1k
views
Assign specific line from a variable to another variable
I would like to take a specific line from a variable to another variable.
I tried this but it doesn't work:
c="1.apple
2.banna
3.peach"
read "Please choose fruit [1-3]:" t
a=$c | awk "NR==$t"
echo "...
3
votes
1
answer
947
views
How to use variable in sed command
I am trying to delete two lines ( 7 and 8) from a .txt file. For this I am using following code:-
#!/bin/sh
Column="7"
sed '"$Column",8d' myfile.txt > result.txt
on running this script, I am ...
3
votes
1
answer
61
views
Awk output to read in a section
I'm trying to output a variable & input to a new command:
jira.sh --action createIssue --project "BLAH" --type "Incident" --summary
"THIS IS A TEST" --components "BLA" --priority "BLAH"| awk '{...
2
votes
2
answers
12k
views
Using the output of `grep` as variable in second command
I currently have 2 bash command line strings I use to gather data needed for a certain task. I was trying to simplify and have only one command used to gather the data without using a script.
First I ...
1
vote
3
answers
10k
views
How to pass directory with escaped space to variable?
I have a small test.sh script as follows:
#!/bin/bash
read -e -p "Enter a Directory: " directory
echo $directory
Here's what happens when I run it:
$ ./test.sh
Enter a Directory: ~/A\ Directory/
/...
1
vote
1
answer
419
views
cron not executing command with variable
I have some logic for executing java projects; it all works in the terminal console when I type it, but not in the cron scheduler:
run 1st microservice and get variable from POST request:
java -jar /...
1
vote
2
answers
124
views
Pass variable IP address to if else
I have this command:
ptr=`host $hostname`
Which results in this:
test.tester.test has address 192.168.1.1
This Works!
What I want now is to extract only the IP address (192.168.1.1), pass it
to ...
1
vote
1
answer
723
views
For with read value
In what way does the final value of number being assigned by read var number (and we enter 5) and number=5 differ? I was making this for loop:
read var number
#number=5
factorial=1
i=1
for i in `seq ...
1
vote
1
answer
69
views
Linux - Get List of Local-User-System Environment Variables Separately
I could get system environment variables by using printenv command, but I need separate variables data:
How could I lists of get local (session-wide), user (user-wide) and system (system-wide, global) ...
1
vote
3
answers
468
views
Extracting data from a variable at the command-line
I have two variables $dc_list and $TEST_VENUE . If I do echo in bash
echo $dc_list
{"active-production-dc":"sc-va1","standby-production-dc":"sc-tx2","active-integration-dc":"int-va1","standby-...
0
votes
1
answer
87
views
Keep file data always available to Bash, without repeated execution (also after reboot)
I have a file named ~/myScripts/assignments.sh which contains various assignments such as variables and aliases. Here's a taste from that file:
drt="/var/www/html"
rss="/etc/init.d/php*-fpm restart &...
0
votes
5
answers
978
views
Return one value and save it into a variable
I have this sentence that returns two values.
cat /ora$sid/dbs/listener.ora|grep PORT|sed 's/.*PORT.*\([0-9]\{4\}\)\(.*\)/\1/'
Return
1521
1522
With this sentence I want to get only one value. This ...
0
votes
1
answer
101
views
Create command line variables with PATH
As I am more of a web guy, i need a little help with this one, so I will explain what I am trying to do before I ask.
I am creating batch script that will:
GET request from an external server (json ...
0
votes
1
answer
127
views
How can I use a variable from a script?
A bash script is running as I defined it in Startup Applications. It is possible to display on terminal a variable used in that script? If yes, how?
0
votes
1
answer
47
views
What happens when we run var=3 command [duplicate]
Below I run what I expected to be an invalid command: var=3 date, which in fact isn't.
$ var=3 date
Sun May 26 17:10:22 UTC 2019
$ echo $?
0
But the variable wasn't assigned the value 3:
$ echo $...
0
votes
1
answer
1k
views
Join multiple columns from multiple variables? [duplicate]
var $abc contains:
abc jkl
def mno
ghi pqr
var $def contains:
stu
vwx
yz
Expected output:
abc jkl stu
def mno vwx
ghi pqr yz
heemayl's solution:
I tried paste <(echo "$abc") <(...
0
votes
3
answers
123
views
How to create a script or a command line that increment the variable part of the name of my files but some of them?
I have several files whose names aren't correct:
$ ls
devoirNote1_1_2.R devoirNote1_1_5.R devoirNote1_4_1.R
devoirNote1_1_3.R devoirNote1_1.R devoirNote1.R
devoirNote1_1_4.R devoirNote1_2_1.R ...