Forum Discussion
depreston
Apr 30, 2024Copper Contributor
Get content from text file and save it into a variable - Azure YML
I have a txt file and I'm trying to save all the content in a variable but I'm only getting the first line of the content into the variable greetings.txt - hello: 123
- hello: 456
- hello: 789 ...
Kidd_Ip
May 19, 2025MVP
How about this:
variables:
- name: MY_TEXT_FILE
value: 'greetings.txt'
readonly: true
- name: GREETINGS_CONTENT
value: ''
steps:
- task: Bash@3
displayName: 'Save text file content in a variable'
inputs:
targetType: 'inline'
script: |
testcontent=$(cat $MY_TEXT_FILE)
export encoded=$(echo "$testcontent" | base64 -w 0)
echo "##vso[task.setvariable variable=GREETINGS_CONTENT]$encoded"
- task: Bash@3
displayName: 'Decode and print variable'
inputs:
targetType: inline
script: |
decoded=$(echo "$GREETINGS_CONTENT" | base64 -d)
echo "My greetings are: $decoded"